Every Kotlin program has an entry point called the function "Main" is. This function marks the start of program execution and is therefore included in every Kotlin program.

Below is a previously familiar section of Kotlin code that prints the message “Hello World!”

 Now let's take a closer look at the structure of the code.

As with other functions in Kotlin, defining the function begins main() with the keyword fun. The abbreviation “fun” stands for the English word “function”. The name of the function is then specified, in this case main.

After the function name, a list of function parameters is given in parentheses. In this example the function main has no parameters, so the brackets are empty.

All actions performed by the function are enclosed in curly brackets (between "{" and "}"). In this specific case, the function prints the message “Hello World!” on the console using the built-in Kotlin function println ().

You can try changing the code yourself and output any text:

In older versions of Kotlin it was also necessary in the main() Function also allows you to enter parameters, as shown in the example below.

The “args: Array” parameter represents an array of strings used to pass the data to the program. The specification of parameters in the function main() is no longer mandatory since version 1.3. However, there is no harm if you have used it in the past and will continue to use it, the code remains fully functional.

Statements 

The Kotlin code essentially consists of instructions, also called statements, which are processed accordingly and trigger certain actions. For example, a statement can result in performing a mathematical operation, reading data from a file, or outputting text to the screen. As a rule, each statement is limited to a specific line in the code. The special feature of Kotlin is that, unlike other similar programming languages ​​such as Java, there is no need for a semicolon at the end of the line. Each statement is simply placed on a new line.

If several statements are written in one line, they must be separated from each other by semicolons.

Comments in the code

To make the program code easier to understand and maintain, it is recommended to provide it with appropriate comments. Comments are ignored by the compiler and have no effect on the execution of the program. In Kotlin, comments are marked with a double slash // for single-line entries or through / * * / for multi-line explanatory texts.

This is what it looks like in real code:

Comments in program code are usually written in English, as English serves as the international lingua franca of the programming world and is understood by most developers worldwide. This makes collaboration in multicultural teams easier and ensures that code is easier to maintain and understand, regardless of the original developer. We follow this unwritten rule and will write comments in our tutorial in English.

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked