Kotlin Tutorial

Kotlin Classes

Kotlin Functions

Function is a block of code which executes when called.

Built-in Functions

In Kotlin,  there are so many built-in functions that you have been using up until now. For example the function main(), println() or print.

Example:

				
					fun main(args: Array<String>) {
   println()
   print("")
}

				
			

Create Functions

You can create a function by using the keyword fun, then simply the name of the function like main() or whatever you want; and simply end it with () parenthesis.

Example:

				
					fun myFirstFunction() {
   println("Hello Im from other function")
}

				
			

The function will output some text inside the println()

Call Functions

If you RUN your code of the example above; nothing will appear in the console except those functions which are inside the main().

We learned “Function is a block of code which executes when called”. Let’s call this myFirstFunction().

Example:

				
					fun main(args: Array<String>) {
   myFirstFunction()
}

fun myFirstFunction() {
   println("Hello I’m from other function")
   }
				
			

Run the code above and you will see the output text which is inside the function (myFirstFunction).

Hello I’m from other function

A function can also be called more than one time (multiple).

Example:

				
					fun main(args: Array<String>) {

   myFirstFunction()
   myFirstFunction()
   myFirstFunction()
}

fun myFirstFunction() {
   println("Hello I’m from other function")
}

				
			

The code above outputs:

				
					Hello I'm from other function
Hello I'm from other function
Hello I'm from other function

				
			

Function Parameters:

Parameters are data, passed in the parentheses of a function as input to the subroutine.

In Kotlin, function parameters are designed using pascal notation, for example name: type, separated by commas.

Example:

				
					fun myFunction(firstName: String, lastName: String) {}
				
			

The function above accepts two parameters – firstname and lastname.

Note:  Each parameter should be explicitly typed.

Let’s modify this myFunction and call it in the main() fun.

Example:

				
					fun main(args: Array<String>) {

   myFunction("John", "Doe")
}

fun myFunction(firstName: String, lastName: String) {
   println("My first name is $firstName and my last name is $lastName")
}

				
			

The code above outputs:

				
					My first name is John and my last name is Doe
				
			

Return a Value:

The return keyword is used to finish the execution of a function; and also to return a value from the function.

To return a value from a function use the keyword return.

Example:

				
					fun main(args: Array<String>) {

   var result = sqrt(5)
   println(result)
}

fun sqrt(num : Int) : Int {
   return num * num
}

				
			

The code above outputs

				
					25
				
			

Put any other value in place of sqrt(5) e.g 6 or 7 to see the square root of the given numbers.

Note: While using return keyword do not forget to explicitly put the return type.

Short way to return a value:

Use “=” operator in place of return without specifying any explicit return type.

Example:

				
					fun main(args: Array<String>) {

   var result = sqrt(5)
   println(result)
}

fun sqrt(num : Int) = num * num
				
			

The above code outputs the same “25”

Learn more about functions, visit Kotlin functions.