<p>In this article, you’ll learn about Kotlin functions, how to define a function and use them in your program with the help of examples.</p>
<h3><span style="color: #000080;"><strong>Functions</strong></span></h3>
<p>A Kotlin <span style="color: #008000;"><strong>function</strong></span> is a collection of statements that are grouped together to perform an operation.</p>
<p>Functions allow us to reuse the code without retyping the code i.e. you can write a method once, and use it multiple times. It also makes code more readable and easier to debug.</p>
<p>In Kotlin, functions can be declared at the top, which means you do not need to create a class to hold a function, which you are required to do in other languages such as <strong>Java, C# or Scala</strong>.</p>
<h3><span style="color: #000080;"><strong>Types of Functions</strong></span></h3>
<p>In Kotlin, depending on whether a function is defined by the user or available in the standard library, functions are of two types:</p>
<ul>
<li>Standard Library Functions</li>
<li>User-defined Functions</li>
</ul>
<h4></h4>
<h4><span style="color: #0000ff;"><strong><span style="color: #000080;">Standard Library Functions</span></strong></span></h4>
<p>Standard library functions are built-in functions in Kotlin that are already defined in standard libraries and available for use.</p>
<p>Examples of Standard library functions are:</p>
<ul>
<li><span style="color: #008000;"><b>sqrt()</b></span> – calculates the square root of a number.</li>
<li><span style="color: #008000;"><b>print()</b></span> – prints the message to standard output.</li>
<li><span style="color: #008000;"><b>max()</b></span> – returns the greater of two values.</li>
<li><span style="color: #008000;"><b>pow()</b></span> – calculates the nth power of a number.</li>
<li><span style="color: #008000;"><b>compareTo()</b></span> – compares two values and return boolean.</li>
<li><span style="color: #008000;"><b>toInt()</b></span> – converts a number to integer value.</li>
</ul>
<h4></h4>
<h5><span style="color: #0000ff;"><strong>For example:</strong></span></h5>
<pre>fun main(args: Array<;String>;) {

 var average = <strong><span style="color: #0000ff;">arrayOf</span></strong>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).<span style="color: #0000ff;"><strong>average()</strong></span>

 <span style="color: #0000ff;"><strong>print</strong></span>("The average of N numbers is: $average")
}</pre>
<h5><strong><span style="color: #0000ff;">Output:</span></strong></h5>
<pre>The average of N numbers is: 5.5</pre>
<h5><strong><span style="color: #0000ff;">Example explained</span></strong></h5>
<ul>
<li><strong><span style="color: #008000;">arrayOf()</span></strong>, <span style="color: #008000;"><strong>average()</strong></span> and <strong><span style="color: #008000;">print()</span></strong> are built-in functions.</li>
<li><span style="color: #008000;"><strong>arrayOf()</strong></span>: require some arguments like integers, double, etc. to create an array.</li>
<li><strong><span style="color: #008000;">average()</span></strong>: used to find the average of values passed to an array.</li>
<li><span style="color: #008000;"><strong>print()</strong></span>: used to print the message to standard output.</li>
</ul>
<h4></h4>
<h4><span style="color: #000080;"><strong>User-defined Functions</strong></span></h4>
<p>A function which is defined by the user is called user-defined function.</p>
<p>To work with user-defined function, firstly we have to define a function then call it or use it.</p>
<h4><span style="color: #000080;"><strong>Defining a method</strong></span></h4>
<p>Before you can use a function (call a method), you need to define it.</p>
<h5><span style="color: #0000ff;"><strong>Syntax:</strong></span></h5>
<p>The syntax of defining a function:</p>
<pre>fun functionName(Parameter list): returnType {
 <strong><span style="color: #008000;"> //Function body</span></strong>
}</pre>
<p>More generally, function declarations have the following components, in order:</p>
<p><span style="color: #0000ff;"><strong>fun:</strong></span> keyword to define a function.</p>
<p><strong><span style="color: #0000ff;">functionName</span></strong>: name of the function, later use to call the function.</p>
<p><span style="color: #000080;"><strong>Note:</strong></span> You can give any name to a function. But it’s more conventional to name it upon the task it performs. As per the naming convention, a method should start with lower case and if it contains more than one word then every word starting should be of upper case.For example: calculateAddition, displayStudentDetails etc.</p>
<p><strong><span style="color: #0000ff;">Parameter list:</span></strong> Parameters are the values passed to a function. You can pass any no of arguments to a function.</p>
<p>In Kotlin, function parameters are defined like this: <span style="color: #008000;"><strong>parameter name: type(datatype)</strong></span></p>
<p><strong><span style="color: #0000ff;">returnType:</span></strong> defines the data type of the value returned by the function or <span style="color: #008000;"><strong>Unit (declaration is optional)</strong></span> if a function does not return a value.</p>
<p><span style="color: #0000ff;"><strong>Function body:</strong></span> The code inside curly braces <strong><span style="color: #008000;">{ }</span></strong> is the body of the function. It defines what the method actually does, how the parameters are manipulated with statements and what values are returned.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<h5><span style="color: #0000ff;"><strong>Example:</strong></span></h5>
<pre>fun <strong><span style="color: #0000ff;">max</span></strong>(<strong><span style="color: #008000;">x: Int, y: Int</span></strong>): <span style="color: #993366;"><strong>Int</strong></span> {
<strong><span style="color: #008000;">//function body</span></strong>
 if (x >; y) {
 println("The greater number is: $x")
 return x
 } else {
 println("The greater number is: $y")
 return y
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Example explained:</strong></span></h5>
<ul>
<li><span style="color: #0000ff;"><strong>max: </strong><span style="color: #000000;"> function name</span></span></li>
<li><strong><span style="color: #008000;">x: Int, y: Int</span><span style="color: #008000;"> (</span></strong><span style="color: #008000;"><span style="color: #000000;"><strong><span style="color: #008000;">parameter list):</span></strong> </span></span>x and y are the parameters name of<span style="color: #008000;"> <strong>Int (integer) </strong></span>data type.</li>
<li><strong><span style="color: #993366;">Int (returnType):</span></strong> defines that <span style="color: #0000ff;"><strong>max()</strong></span> function returns integer value.</li>
</ul>
<h3></h3>
<h4><span style="color: #000080;"><strong>Single-expression functions</strong></span></h4>
<p>In Kotlin, when a function returns a single expression, the curly braces can be omitted and the body is specified after a <span style="color: #000080;"><strong>=</strong></span> symbol:</p>
<pre>fun max(x: Int, y: Int): <span style="color: #0000ff;"><strong>Int</strong></span> = if (x >; y) x else y

fun add(x: Double, y: Double): <strong><span style="color: #0000ff;">Double</span></strong> = x + y

fun data(x: Int): <strong><span style="color: #0000ff;">Unit</span></strong> = print("Value of x is $x")</pre>
<p><span style="color: #000080;"><strong>Note:</strong></span> Functions with block body must always specify return types explicitly while working with single-expression functions declaring the return type explicitly is optional.</p>
<pre>fun max(x: Int, y: Int) = if (x >; y) x else y

fun add(x: Double, y: Double) = x + y

fun data(x: Int) = print("Value of x is $x")</pre>
<h3></h3>
<h4><span style="color: #000080;"><strong>Calling a method</strong></span></h4>
<p>Now we have defined a method, we need to use it. For that, we have to call the method to use its functionality.</p>
<h5><span style="color: #0000ff;"><strong>Syntax:</strong></span></h5>
<p>Syntax of calling a method:</p>
<pre>functionName(Parameter list)</pre>
<p>The way of calling the<strong> <span style="color: #008000;">max()</span></strong> method which we have defined above:</p>
<pre>max(5 , 9)</pre>
<p>In a program, when a compiler comes to a line containing the function call, control jumps to the definition of that function and executes all the instructions one by one.</p>
<p>Control is transferred back only when the function reaches closing braces or there any <span style="color: #008000;"><strong>return</strong></span> statement.</p>
<h5><span style="color: #0000ff;"><strong>Example: </strong></span></h5>
<pre><strong><span style="color: #008000;">//defining max() function</span></strong>
fun max(x: Int, y: Int): Int {
 if (x >; y) {
 println("The greater number is: $x")
 return x
 } else {
 println("The greater number is: $y")
 return y
 }
}

fun main(args: Array<;String>;) {
<strong><span style="color: #008000;"> //calling max() function</span></strong>
 var result = max(9, 5)
 println("The maximum of two numbers 5 and 9 is $result")
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>The greater number is: 9
The maximum of two numbers 5 and 9 is 9</pre>
<p> ;</p>
<h3><span style="color: #000080;"><strong>Example: Kotlin Functions</strong></span></h3>
<pre>class Addition {
 var result = 0

 <strong><span style="color: #008000;">//addTwoNumbers() function definition</span></strong>
<strong><span style="color: #008000;"> //function with two parameters and</span></strong>
<strong><span style="color: #008000;"> //returning integer value.therefore,returnType is Int</span></strong>
 fun addTwoNumbers(a: Int, b: Int): Int {

 <strong><span style="color: #008000;">// adding two values.</span></strong>
 result = a + b

 <strong><span style="color: #008000;">//calling display() method within the same class</span></strong>
 display()

 return result
 }

 <strong><span style="color: #008000;">//display() function definition</span></strong>
<strong><span style="color: #008000;"> //function with no parameter and</span></strong>
<strong><span style="color: #008000;"> //returning nothing.therefore,returnType is Unit
</span> <span style="color: #008000;"> //Unit(returnType) declaration is optional</span></strong>
 fun display(): Unit {
 println("display() in Addition class calling inside addTwoNumbers()")
 }
}

fun main(args: Array<;String>;) {
 <span style="color: #008000;"><strong>// creating an instance of Addition class</strong></span>
 val addition = Addition()

 <strong><span style="color: #008000;">// calling addTwoNumbers() method to add two values</span></strong>
<strong><span style="color: #008000;"> // using instance created in above step.</span></strong>
 val result = addition.addTwoNumbers(10, 5)
 println("Addition of two Numbers :$result")
}</pre>
<h5><span style="color: #0000ff;"><strong>Output:</strong></span></h5>
<pre>display() in Addition class calling inside addTwoNumbers()
Addition of two Numbers :15</pre>
<h4></h4>
<h5><span style="color: #0000ff;"><strong>Control flow of above program:</strong></span></h5>
<p><img class=" size-full wp-image-1818 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/04/kotlin_func_img.png" alt="kotlin_func_img" width="1031" height="602" />

