Kotlin Functions

A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are also known as methods.
So it turns out you already know what a function is. You have been using it 
the whole time through this tutorial!
For example, println() is a function. It is used to output/print text to the screen:
fun main() {
  println("Hello World")
}
To create your own function, use the fun keyword, and write the name of the 
function, followed by parantheses ():
Create a function named "myFunction" that should output some text:
fun myFunction() {
  println("I just got executed!")
} 
Now that you have created a function, you can execute it by calling 
it.
To call a function in Kotlin, write the name of the function followed by two 
parantheses ().
In the following example, myFunction() will 
print some text (the action), when it is called:
fun main() {
  myFunction() // Call myFunction
}

// Outputs "I just got executed!" 
A function can be called multiple times, if you want:
fun main() {
  myFunction()
  myFunction()
  myFunction()
}

// I just got executed!
// I just got executed!
// I just got executed! 
Information can be passed to functions as parameter.
Parameters are specified after the function name, inside the parentheses.
You can add as many parameters as you want, just separate them with a comma. 
Just note that you must specify the type of each parameter (Int, String, etc). 

The following example has a 
function that takes a String called fname as parameter.
When the function is called, we pass along a first name,
which is used inside the function to print the full name:

fun myFunction(fname: String) {
  println(fname + " Doe")
}

fun main() {
  myFunction("John")
  myFunction("Jane")
  myFunction("George")
}
  
// John Doe
// Jane Doe
// George Doe 
When a parameter is passed to the function, it is called an argument. So, from the example above: fname is a parameter, while 
John, Jane and 
George are arguments.
You can have as many parameters as you like:
fun myFunction(fname: String, age: Int) {
  println(fname + " is " + age)
}

fun main() {
  myFunction("John", 35)
  myFunction("Jane", 32)
  myFunction("George", 15)
}

// John is 35
// Jane is 32
// George is 15 
Note: When working with multiple parameters, the function call must 
have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
In the examples above, we used functions to output a value. In the following example, we will use a function to 
return a value and assign it to a variable.
To return a value, use the return keyword, and specify the 
return type after 
the function's parantheses (Int in this example):
A function with one Int parameter, and Int return type:
fun myFunction(x: Int): Int {
  return (x + 5)
}

fun main() {
  var result = myFunction(3)
  println(result)
}

// 8 (3 + 5) 
Using two parameters:
A function with two Int parameters, and Int return type:
fun myFunction(x: Int, y: Int): Int {
  return (x + y)
}

fun main() {
  var result = myFunction(3, 5)
  println(result)
}

// 8 (3 + 5) 
There is also a shorter syntax for returning values. You can use the = operator instead of return 
without specifying the return type. Kotlin is smart enough to automatically find out what it is:
fun myFunction(x: Int, y: Int) = x + y

fun main() {
  var result = myFunction(3, 5)
  println(result)
}

// 8 (3 + 5) 
We just launchedW3Schools videos
Get certifiedby completinga course today!
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
help@w3schools.com
Your message has been sent to W3Schools.