A function is a part of a program that performs a specific task or calculation and, if necessary, returns a result. Functions are used to divide the code into logical units, which allows the code to be reusable and clear.
In other words, a function can be viewed as a type of “subprogram” that performs specific tasks within a larger program and can return the result of those tasks to the main program. It helps make the code modular by combining repeated tasks into a single function that can be called from multiple places in the program.
In Kotlin, the syntax of a function is defined as follows:
- The key word is used to declare a function.
- This is followed by the name of the function.
- The parameters are placeholders and are used to pass data or values (also called arguments) to the function for processing. Function parameters are written in parentheses and each parameter has a name and a type. Multiple parameters are separated by commas. The definition of parameters is optional.
- If the function has a return value, the return type can be specified after the list of parameters. If no return type is specified, the function will be a function of type Unit angesehen.
- Finally, in the curly brackets is the function body - the code that is executed when the function is called and does the actual task.
- If a function should return a value, the keyword can return be used to pass the value to the caller.
Here is an example of a function that adds two numbers and returns the result:
Organization of functions within a program
The main()-Function We got to know them at the beginning of the tutorial and have been using them regularly in the examples and exercises since then. This is necessary to even be able to run the Kotlin code.
Short remembering: The function main() in Kotlin is the main function of a Kotlin program. It is the entry point of the program and is called first when it is executed.
Until now we always have our functions inside the main function main() placed, which can also be useful to enable certain functionalities in the main()-Function to group and organize. However, in software development, especially for more complex applications, it is common to organize functions in a logical and clear way by, among other things as independent functions (top-level functions) both outside the main()function as well as defined outside of classes or even outsourced to a separate file. These functions are only called within the main function when they are really needed.
Now let's reorganize the code from the previous example accordingly:
From now on we will use this notation more often.
Functions without parameters
A function without parameters is a function that does not require any input data to perform its task. In Kotlin syntax, such functions are declared without a parameter list (the brackets remain empty functionName() ).
For example, we can define and call a function that simply prints a specific string to the console:
In the previous example, the function has sayHelloWorld no parameters, just outputs text to the console.
Parameters can have different data types within a function
In Kotlin, functions can have parameters with different data types. This flexibility makes it possible to write functions that can handle a variety of different data inputs.
For example, a function can have a String parameter, an Integer parameter and a Boolean parameter. Here is an example:
Default arguments for function parameters
Default arguments are predefined values for function parameters. If a function call does not specify a value for a particular parameter, the function uses the default value for that parameter instead. These default values can be specified in the function definition and make it easier for developers to make the functions more flexible and reusable.
The syntax for using default arguments for function parameters is as follows:
- When defining the function, the parameter is given a default argument using the equal sign = assigned.
The following example shows the use of default values in Kotlin:
Named arguments
The values are passed to the parameters by default according to their position: The first value is passed to the first parameter, the second value to the second parameter, etc. By using it named arguments the transfer order can be changed. You do this by specifying the name of the parameter within the brackets when calling the function and passing its value using the equal sign.
It is not necessary to pass all arguments by name. Some of the arguments can also be passed via position. However, if an argument was passed by name, all subsequent arguments must also be passed by the name of the corresponding parameter.
If there are optional parameters before a mandatory function parameter, then a value must be passed for the mandatory parameter using its name.
Change of function parameters
By default, in Kotlin, function parameters are saved as val Variables are declared, which means that their value cannot be changed. Trying to change the value of such a parameter within the function will throw an error. Here's an example:
However, if you pass a complex object, such as an array, as a function parameter, you can change its contents within the function. Below is an example: