Functions perform some specific tasks and help in improving the code’s clarity and reducing the duplication of code by increasing the reusability of the code. In this tutorial, you will look into all about Kotlin functions with the help of examples.
Kotlin Functions
A function is a block of code with a name that holds the data or the information. This block of code can easily be invoked by calling it from the main function. Functions in Kotlin are defined with the fun keyword with the function's name.
A function can easily be invoked by calling it, using the name of the function with parentheses, for example, Data().
Now, understand Kotlin Function parameters.
Function Parameters
Functions can also accept the data or information passed to them; this data is passed to the function's parameters, which are specified after the name of the function. You must specify the type of the parameter along with its name.
For example:
Here the function Product takes n1 and n2 as parameters of type Int.
Now, have a look at function returning values.
Function Returning Values
A function can also return values; to return a value, the return keyword is used, and the return type is specified after the function’s parenthesis.
For example:
As you can see in the example above, the return type of the function Int is specified after the function’s parenthesis. Also, if the function doesn't return any value, then it is called a Unit returning function, i.e. the return type is Unit.
The output of the above example is 35.
Now, you will understand the next topic of Kotlin Functions.
Higher Order Functions
In Kotlin language, there is a function that can accept the function as a parameter or can also return a function; that particular function is known as the Higher-order function.
Normally, functions accept or pass integers, strings, and arrays, and their syntax is as follows:
In case of higher-order functions.
As you can see, the higher order function accepts two integer values and a function named funcName which will accept two integer values and return the integer value.
To pass a function in a higher-order function, you use two colon operators with the function name. The colons are used with the function name when we want to pass the function as a parameter.
The output of the above program is 100.
Lambda Function
Lambda is a short way of writing a function. The lambda function is written with curly braces {}, and it doesn’t have any name. The basic syntax of the Lambda function is:
The function body is written after the variable with an arrow operator.
Here 9 and 3 are passed to variables n1 and n2. There are variables before the ->(arrow operator), and after the arrow, there is a function body.
Below is the output of the above example.
Now you will understand how to use the Lambda function with the higher-order function.
As you have already done the example of a higher-order function, in that example, you can eliminate the code and write the code more compactly with the help of the lambda function.
Now, have a look at the code of the higher-order function written with the help of the Lambda function.
As you can see, you have added the lambda function in place of two colon operators, and you have also eliminated the myFunction function.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
Conclusion
In this tutorial on Kotlin Functions, you understood function parameters and learned what Kotlin Functions are. You also learned about the Function returning values and understood the higher order functions. At last, you understood Lambda functions and used lambda functions with higher order functions.
If you are looking to build a software development career, you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you build your career in the right direction.
Do you have any questions regarding this tutorial on Kotlin Multiplatform? If you do, then you can put them in the comments section. We’ll help you solve your queries.