Site icon C1CTech

Kotlin Functions

<p>In this article&comma; you’ll learn about Kotlin functions&comma; how to define a function and use them in your program with the help of examples&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Functions<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>A Kotlin <span style&equals;"color&colon; &num;008000&semi;"><strong>function<&sol;strong><&sol;span> is a collection of statements that are grouped together to perform an operation&period;<&sol;p>&NewLine;<p>Functions allow us to reuse the code without retyping the code i&period;e&period; you can write a method once&comma; and use it multiple times&period; It also makes code more readable and easier to debug&period;<&sol;p>&NewLine;<p>In Kotlin&comma; functions can be declared at the top&comma; which means you do not need to create a class to hold a function&comma; which you are required to do in other languages such as <strong>Java&comma; C&num; or Scala<&sol;strong>&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Types of Functions<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>In Kotlin&comma; depending on whether a function is defined by the user or available in the standard library&comma; functions are of two types&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Standard Library Functions<&sol;li>&NewLine;<li>User-defined Functions<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;000080&semi;">Standard Library Functions<&sol;span><&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Standard library functions are built-in functions in Kotlin that are already defined in standard libraries and available for use&period;<&sol;p>&NewLine;<p>Examples of Standard library functions are&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><b>sqrt&lpar;&rpar;<&sol;b><&sol;span> – calculates the square root of a number&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><b>print&lpar;&rpar;<&sol;b><&sol;span> – prints the message to standard output&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><b>max&lpar;&rpar;<&sol;b><&sol;span> – returns the greater of two values&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><b>pow&lpar;&rpar;<&sol;b><&sol;span> – calculates the nth power of a number&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><b>compareTo&lpar;&rpar;<&sol;b><&sol;span> – compares two values and return boolean&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><b>toInt&lpar;&rpar;<&sol;b><&sol;span> – converts a number to integer value&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>For example&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; var average &equals; <strong><span style&equals;"color&colon; &num;0000ff&semi;">arrayOf<&sol;span><&sol;strong>&lpar;1&comma; 2&comma; 3&comma; 4&comma; 5&comma; 6&comma; 7&comma; 8&comma; 9&comma; 10&rpar;&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>average&lpar;&rpar;<&sol;strong><&sol;span>&NewLine;&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>print<&sol;strong><&sol;span>&lpar;"The average of N numbers is&colon; &dollar;average"&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output&colon;<&sol;span><&sol;strong><&sol;h5>&NewLine;<pre>The average of N numbers is&colon; 5&period;5<&sol;pre>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h5>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">arrayOf&lpar;&rpar;<&sol;span><&sol;strong>&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>average&lpar;&rpar;<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">print&lpar;&rpar;<&sol;span><&sol;strong> are built-in functions&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>arrayOf&lpar;&rpar;<&sol;strong><&sol;span>&colon; require some arguments like integers&comma; double&comma; etc&period; to create an array&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">average&lpar;&rpar;<&sol;span><&sol;strong>&colon; used to find the average of values passed to an array&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>print&lpar;&rpar;<&sol;strong><&sol;span>&colon; used to print the message to standard output&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>User-defined Functions<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>A function which is defined by the user is called user-defined function&period;<&sol;p>&NewLine;<p>To work with user-defined function&comma; firstly we have to define a function then call it or use it&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Defining a method<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Before you can use a function &lpar;call a method&rpar;&comma; you need to define it&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>The syntax of defining a function&colon;<&sol;p>&NewLine;<pre>fun functionName&lpar;Parameter list&rpar;&colon; returnType &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Function body<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p>More generally&comma; function declarations have the following components&comma; in order&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fun&colon;<&sol;strong><&sol;span> keyword to define a function&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">functionName<&sol;span><&sol;strong>&colon; name of the function&comma; later use to call the function&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Note&colon;<&sol;strong><&sol;span> You can give any name to a function&period; But it’s more conventional to name it upon the task it performs&period; As per the naming convention&comma; a method should start with lower case and if it contains more than one word then every word starting should be of upper case&period;For example&colon; calculateAddition&comma; displayStudentDetails etc&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Parameter list&colon;<&sol;span><&sol;strong> Parameters are the values passed to a function&period; You can pass any no of arguments to a function&period;<&sol;p>&NewLine;<p>In Kotlin&comma; function parameters are defined like this&colon; <span style&equals;"color&colon; &num;008000&semi;"><strong>parameter name&colon; type&lpar;datatype&rpar;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">returnType&colon;<&sol;span><&sol;strong> defines the data type of the value returned by the function or <span style&equals;"color&colon; &num;008000&semi;"><strong>Unit &lpar;declaration is optional&rpar;<&sol;strong><&sol;span> if a function does not return a value&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Function body&colon;<&sol;strong><&sol;span>  The code inside curly braces <strong><span style&equals;"color&colon; &num;008000&semi;">&lbrace; &rcub;<&sol;span><&sol;strong> is the body of the function&period; It defines what the method actually does&comma; how the parameters are manipulated with statements and what values are returned&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>fun <strong><span style&equals;"color&colon; &num;0000ff&semi;">max<&sol;span><&sol;strong>&lpar;<strong><span style&equals;"color&colon; &num;008000&semi;">x&colon; Int&comma; y&colon; Int<&sol;span><&sol;strong>&rpar;&colon; <span style&equals;"color&colon; &num;993366&semi;"><strong>Int<&sol;strong><&sol;span> &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;function body<&sol;span><&sol;strong>&NewLine; if &lpar;x &gt&semi; y&rpar; &lbrace;&NewLine; println&lpar;"The greater number is&colon; &dollar;x"&rpar;&NewLine; return x&NewLine; &rcub; else &lbrace;&NewLine; println&lpar;"The greater number is&colon; &dollar;y"&rpar;&NewLine; return y&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>max&colon; <&sol;strong><span style&equals;"color&colon; &num;000000&semi;"> function name<&sol;span><&sol;span><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">x&colon; Int&comma; y&colon; Int<&sol;span><span style&equals;"color&colon; &num;008000&semi;"> &lpar;<&sol;span><&sol;strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">parameter list&rpar;&colon;<&sol;span><&sol;strong>  <&sol;span><&sol;span>x and y are the parameters name of<span style&equals;"color&colon; &num;008000&semi;"> <strong>Int &lpar;integer&rpar; <&sol;strong><&sol;span>data type&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;993366&semi;">Int &lpar;returnType&rpar;&colon;<&sol;span><&sol;strong> defines that <span style&equals;"color&colon; &num;0000ff&semi;"><strong>max&lpar;&rpar;<&sol;strong><&sol;span> function returns integer value&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Single-expression functions<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>In Kotlin&comma; when a function returns a single expression&comma; the curly braces can be omitted and the body is specified after a  <span style&equals;"color&colon; &num;000080&semi;"><strong>&equals;<&sol;strong><&sol;span>  symbol&colon;<&sol;p>&NewLine;<pre>fun max&lpar;x&colon; Int&comma; y&colon; Int&rpar;&colon; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Int<&sol;strong><&sol;span> &equals; if &lpar;x &gt&semi; y&rpar; x else y&NewLine;&NewLine;fun add&lpar;x&colon; Double&comma; y&colon; Double&rpar;&colon; <strong><span style&equals;"color&colon; &num;0000ff&semi;">Double<&sol;span><&sol;strong> &equals; x &plus; y&NewLine;&NewLine;fun data&lpar;x&colon; Int&rpar;&colon; <strong><span style&equals;"color&colon; &num;0000ff&semi;">Unit<&sol;span><&sol;strong> &equals; print&lpar;"Value of x is &dollar;x"&rpar;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Note&colon;<&sol;strong><&sol;span> Functions with block body must always specify return types explicitly while working with single-expression functions declaring the return type explicitly is optional&period;<&sol;p>&NewLine;<pre>fun max&lpar;x&colon; Int&comma; y&colon; Int&rpar; &equals; if &lpar;x &gt&semi; y&rpar; x else y&NewLine;&NewLine;fun add&lpar;x&colon; Double&comma; y&colon; Double&rpar; &equals; x &plus; y&NewLine;&NewLine;fun data&lpar;x&colon; Int&rpar; &equals; print&lpar;"Value of x is &dollar;x"&rpar;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Calling a method<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Now we have defined a method&comma; we need to use it&period; For that&comma; we have to call the method to use its functionality&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<p>Syntax of calling  a method&colon;<&sol;p>&NewLine;<pre>functionName&lpar;Parameter list&rpar;<&sol;pre>&NewLine;<p>The way of calling the<strong> <span style&equals;"color&colon; &num;008000&semi;">max&lpar;&rpar;<&sol;span><&sol;strong> method which we have defined above&colon;<&sol;p>&NewLine;<pre>max&lpar;5 &comma; 9&rpar;<&sol;pre>&NewLine;<p>In a program&comma; when a compiler comes to a line containing the function call&comma; control jumps to the definition of that function and executes all the instructions one by one&period;<&sol;p>&NewLine;<p>Control is transferred back only when the function reaches closing braces or there any <span style&equals;"color&colon; &num;008000&semi;"><strong>return<&sol;strong><&sol;span> statement&period;<&sol;p>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example&colon; <&sol;strong><&sol;span><&sol;h5>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;defining max&lpar;&rpar; function<&sol;span><&sol;strong>&NewLine;fun max&lpar;x&colon; Int&comma; y&colon; Int&rpar;&colon; Int &lbrace;&NewLine; if &lpar;x &gt&semi; y&rpar; &lbrace;&NewLine; println&lpar;"The greater number is&colon; &dollar;x"&rpar;&NewLine; return x&NewLine; &rcub; else &lbrace;&NewLine; println&lpar;"The greater number is&colon; &dollar;y"&rpar;&NewLine; return y&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;calling max&lpar;&rpar; function<&sol;span><&sol;strong>&NewLine; var result &equals; max&lpar;9&comma; 5&rpar;&NewLine; println&lpar;"The maximum of two numbers 5 and 9 is &dollar;result"&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>The greater number is&colon; 9&NewLine;The maximum of two numbers 5 and 9 is 9<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Example&colon; Kotlin Functions<&sol;strong><&sol;span><&sol;h3>&NewLine;<pre>class Addition &lbrace;&NewLine; var result &equals; 0&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;addTwoNumbers&lpar;&rpar; function definition<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;function with two parameters and<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;returning integer value&period;therefore&comma;returnType is Int<&sol;span><&sol;strong>&NewLine; fun addTwoNumbers&lpar;a&colon; Int&comma; b&colon; Int&rpar;&colon; Int &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; adding two values&period;<&sol;span><&sol;strong>&NewLine; result &equals; a &plus; b&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;calling display&lpar;&rpar; method within the same class<&sol;span><&sol;strong>&NewLine; display&lpar;&rpar;&NewLine;&NewLine; return result&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;display&lpar;&rpar; function definition<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;function with no parameter and<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;returning nothing&period;therefore&comma;returnType is Unit&NewLine;<&sol;span> <span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Unit&lpar;returnType&rpar; declaration is optional<&sol;span><&sol;strong>&NewLine; fun display&lpar;&rpar;&colon; Unit &lbrace;&NewLine; println&lpar;"display&lpar;&rpar; in Addition class calling inside addTwoNumbers&lpar;&rpar;"&rpar;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; creating an instance of Addition class<&sol;strong><&sol;span>&NewLine; val addition &equals; Addition&lpar;&rpar;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; calling addTwoNumbers&lpar;&rpar; method to add two values<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; using instance created in above step&period;<&sol;span><&sol;strong>&NewLine; val result &equals; addition&period;addTwoNumbers&lpar;10&comma; 5&rpar;&NewLine; println&lpar;"Addition of two Numbers &colon;&dollar;result"&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<pre>display&lpar;&rpar; in Addition class calling inside addTwoNumbers&lpar;&rpar;&NewLine;Addition of two Numbers &colon;15<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Control flow of above program&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<p><img class&equals;" size-full wp-image-1818 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;kotlin&lowbar;func&lowbar;img&period;png" alt&equals;"kotlin&lowbar;func&lowbar;img" width&equals;"1031" height&equals;"602" &sol;>&NewLine;

Exit mobile version