What is Functions?

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 two numbers. So, the function avoids the repetition of code and makes the code more reusable.

Note: There is a space between closing bracket ‘)’ and opening ‘{‘ .

Kotlin functions

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. 

Learn Kotlin Functions

  • What is Functions?
  • Example of a Function
  • Types of Functions in Kotlin
  • 1. Kotlin User-Defined Function
  • 2. Kotlin Standard Library Function
  • Advantages and Disadvantages in Kotlin Function

Similar Reads

What is Functions?

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....

Example of a Function

For example: If we have to compute the sum of two numbers then define a fun sum()....

Types of Functions in Kotlin

In Kotlin, there are two types of functions:...

1. Kotlin User-Defined Function

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....

2. Kotlin Standard Library Function

In Kotlin, there are different numbers 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 below program, we will use built-in functions arrayOf(), sum() and println(). The function arrayOf() requires some arguments like integers, double, etc to create an array and we can find the sum of all elements using sum() which does not require any argument....

Advantages and Disadvantages in Kotlin Function

-> Advantages of Using functions in Kotlin...