A function is a unit of code that performs a specific function. In programming, the function divides the code into smaller modules, which makes the program more manageable.
Therefore, the function avoids duplication of the code and makes the code more reusable. In Kotlin, there are two types of functions:
In Kotlin, the standard library has various built-in functions already defined and available for use. We can pass arguments as needed and call them. In the following program, we will use the built-in functions arrayOf (), sum (), and println (). The function arrayOf () requires some arguments, such as integers, doubles, etc., to create an array, and we can find the sum of all the elements using the sum () without argument.
List of different standard library functions and their uses -
A function defined by the user is called a user-defined function. As we know, we need to define a function to divide an extensive program into smaller modules. Each defined function has its characteristics, such as the function name, the function's return type, and the number of parameters transferred to the function.
In Kotlin, the function can be declared above, and you do not have to create a class to hold a function, which we use to do in other languages, such as Java or Scala. fun- For defining a function.
It is used to create a function to perform a specific task. Whenever it is called a function, it exits the current section of the program code and starts executing the function. Flow-control of a function is as follows:
|