Every Kotlin program has at least one function main(). However, you can define any number of functions in your program until memory is not full. It’s always a good practice to divide the whole program into smaller functions that are logically similar. It makes program more readable, understandable and debuggable.
A function declaration is done using the fun keyword.
The basic syntax of a function in Kotlin looks like this:
Syntax of for loop
A function has a name, which is used to refer to it, in other words, to call the function. After the name comes the list of function parameters.
Following the brackets is the data type that should be returned by the function. This can be anything, as long as the variable being returned matches that type.
When the function is called, all of the statements within the curly brackets are run.
How to call a user defined function in kotlin?
While creating a function, we assign some specified task that function will perform whenever it is called. Whenever a function is called, control is transferred to that function. Then, specified task is
performed. After that, control is transferred back to the caller of the function. Control is transferred only when either function reaches to closing bracket ( ‘}’ ) or there is a return statement that returns
from callee function to caller function.
Output Value = 15
Here, we have defined a function multiply() that accepts 2 integer values. Then, multiply those values and return the result. We are calling this method by it’s name (i.e. multiply()) and passing two
values (3 and 5) in it that are required to call the function. Then, multiply() method is called and result is returned to the main function. The result returned form multiply() method is assigned to
the variable multipliedValue. Then, This value is printed using println() method.
Actual and Formal arguments in function
When we define a function, we mention what are the parameters that the function will accept. We also mention what are the data types of the arguments accepted by the function etc.
Output Value = 16.5
Here, we have defined a function multiply() that multiplies two numbers. It accepts two values as parameters and then multiply these two values. Then, return the result got after multiplication. We are calling this function from main() functions. After that, result returned from multiply() method is printed.
Value of variable a will be assigned to variable first in multiply() function. Value of variable b will be assigned to variable second in multiply() function.
Output Value = 15.0
Arguments passed to the function while calling it are called actual arguments. Here, a and b are actual arguments.
Also, Arguments defined as a parameters of the function are called formal arguments. Here, first and second are formal arguments.
Note that, data types of actual arguments must match with data types of formal arguments in same order.
In the above program, we have called the function as multiply(a, b). Note that data type of a is Int while data type of b is Float. In the same order, data type of first is Int while data type of second is Float.
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂