![]() |
VOOZH | about |
In Kotlin, functions are used to encapsulate a piece of behavior that can be executed multiple times. Functions can accept input parameters, return values, and provide a way to encapsulate complex logic into reusable blocks of code.
Table of Content
A function is a unit of code that performs a special task. In programming, the function is used to break the code into smaller modules, which makes the program more manageable.
We can call sum(x, y) at any number of times and it will return the sum of the two numbers. So, the function avoids the repetition of code and makes the code more reusable.
If we have to compute the sum of two numbers, then define a fun sum().
Function parameters are defined as the elements that are passed to the function for further processing, where operations are performed.
In the above example, there are two parameters mentioned:
Here in the body, we perform the operations to be performed in the function, like in the above Example:
val c = a + bReturn value is the value which is returned after the operation is performed in the function. In above example c is the value which is returned.
return cIn Kotlin, there are two types of functions:
A function that is defined by the user is called a user-defined function. As we know, to divide a large program into small modules we need to define function. Each defined function has its own properties like the name of the function, return type of a function, number of parameters passed to the function, etc.
In Kotlin, the function can be declared at the top, and no need to create a class to hold a function, which we are used to doing in other languages such as Java or Scala. Generally, we define a function as:
fun fun_name(a: data_type, b: data_type, ......): return_type {
// other codes
return
}
Explanation: We have defined a function above starting with the fun keyword whose return type is an Integer.
>> mul() is the name of the function
>> num1 and num2 are names of the parameters being accepted by the function
and both are Integer type.
Explanation: We have defined a function using the fun keyword whose return type is Unit by default.
>> student is the name of the function.
>> name is the parameter of String data type.
>> roll_no is the parameter of Integer data type
>> grade is the parameter of Character data type
We create a function to assign a specific task. Whenever a function is called the program leaves the current section of code and begins to execute the function.
The flow control of a function:
Kotlin program to call the mul() function by passing two arguments:
Output:
The multiplication of two numbers is: 15 Explanation: In the above program, we are calling the mul(3, 5) function by passing two arguments. When the function is called, the control transfers to the mul() and starts execution of the statements in the block. Using built-in times(), it calculates the multiple of two numbers and stores it in a variable. Then it exits the function by returning the integer value and controls transfer back to the main(), where it calls mul(). Then we store the value returned by the function into a mutable variable result, and println() prints it to the standard output.
Kotlin program to call the student() function by passing all arguments:
Output:
Name of the student is: Praveen
Grade of the student is: A
Roll no of the student is: 25
Name of the student is: Gaurav
Grade of the student is: B
Roll no of the student is: 30
Explanation: In the above program, we are calling the student() function by passing the arguments in the same order as required. If we try to jumble the arguments then it gives the type mismatch error. In the first call, we pass the argument using variables and in the second call, we pass the arguments values without storing in variables. So, both methods are correct to call a function.
In Kotlin, there are different number of built-in functions already defined in the standard library and available for use. We can call them by passing arguments according to requirements. In the program below, we will use built-in functions arrayOf(), sum(), and println(). The function arrayOf() requires some arguments like integers, doubles, etc, to create an array, and we can find the sum of all elements using sum(), which does not require any argument.
Below is the implementation of the standard library function:
Output:
The sum of all the elements of an array is: 55In the program below, we will use rem() to find the remainder.
Output:
The remainder when 26 is divided by 3 is: 2The list of different standard library functions and their use :