
In this post, you will learn about Kotlin functions; their definition, syntax, and how to create functions.
First, What is a Function?
A function is a group of statements that performs a certain task. Functions are also known as methods.
Here is the syntax of a function.

To create a function, use the fun keyword, write the name of the function (identifier), followed by parentheses ( ). We write parameters inside the parentheses. We talk about parameters in a moment. Next, we write colon : and return type, followed by curly brackets { }. Inside these brackets, we write the instructions. They are also known as the body of the function. All of these things combined are called the function definition.
What are function parameters?
We can pass data to a function. This data is called a parameter. Multiple parameters are separated by commas.
Now, let’s understand the above definition with examples. Let’s create a function that takes first and last names and returns the full name.
fun getFullName (firstName: String, lastName: String): String {
return "$firstName $lastName"
}
Let’s compare it with function syntax.

Here, getFullName is the function’s name, firstName and lastName are parameters of type String. The return type is also a String. Inside its body, it concatenates firstName and lastName and returns the result. We use the return keyword before the value that is being returned by the function.
You have created a function, but how do you execute it? For that, simply write the name of the function followed by parentheses ( ). Write the values of the parameters inside the parentheses. It is called calling the function.
getFullName("Michael", "Smith")
We passed Michael and Smith to the function. These are called arguments. Here Michael will be assigned to firstName and Smith will be assigned to lastName.

Our getFullName method returns a value. So, let’s store the value in a variable and print it on the console.
val fullName = getFullName("Michael", "Smith")
println(fullName)
Our final code looks like this.
fun main() {
val fullName = getFullName("Michael", "Smith")
println(fullName)
}
fun getFullName(firstName: String, lastName: String): String {
return "$firstName $lastName"
}
Run it and it prints Michael Smith on the console.
Difference Between Parameter and Argument:
A parameter is listed inside the parentheses in the function definition. In the above example, firstName and lastName are the parameters.
An argument is a value that is sent to the function when it is called. In the above example, Michael and Smith are the arguments.
You need to follow 3 rules while passing the arguments:
- The number of parameters must be the same as the number of arguments.
- The arguments must be passed in the same order.
- The data types of the arguments and parameters should match.
You can call a function as many times as you want. This is the main advantage of the functions. They help you avoid writing the same code again and again and make your code more readable and organized. Let’s call the above method 3 times.
fun main() {
val fullName1 = getFullName("Michael", "Smith")
println(fullName1)
val fullName2 = getFullName("David", "Miller")
println(fullName2)
val fullName3 = getFullName("Robert", "Jones")
println(fullName3)
}
fun getFullName(firstName: String, lastName: String): String {
return "$firstName $lastName"
}
Output:
Michael Smith
David Miller
Robert Jones
Shorter Syntax:
If the function returns a single expression, you can remove the return type, curly brackets, and the return keyword. So, the shorter version of the getFullName method is:
fun getFullName(firstName: String, lastName: String) = "$firstName $lastName"
Note: The return type and parameters are optional. If the function doesn’t return anything, its return type is Unit. It is optional to write Unit.
Let’s create a function that has no return type and parameters.
fun printWelcome() {
println("Hi, Welcome to SemicolonSpace")
}
Let’s call this function in the main method.
fun main() {
printWelcome()
}
fun printWelcome() {
println("Hi, Welcome to SemicolonSpace")
}
Output:
Hi, Welcome to SemicolonSpace
Types of Functions:
Depending on if a function is created by the user, or available in the standard library, there are two types of functions:
- Standard library functions
- User-defined functions
Standard Library Functions:
If you observe the above code, you will find that there are extra two functions. They are main and println. These are Kotlin standard library functions. They are built-in functions and readily available for use.
The main method is like the entry point of the code. The Kotlin compiler starts executing the code from the main function. It can be related to main() in Java or C.
The println method is used to print the message on the console (output).
User-defined Functions:
The functions we create are called user-defined functions. For example, in the above code, printWelcome is the user-defined function because we created it.
Advantages of using Functions:
- No need to write the same code again and again
- Makes your code more readable and organized
- You can break the larger program into small chunks