Beginner 8 min readKotlin 2.0

Kotlin Functions — Declaration, Parameters, and Return

Functions are reusable blocks of code. In Kotlin, you declare them with the fun keyword. This tutorial covers everything from basic functions to return values.

What You Will Learn

  • Declare functions with fun
  • Add parameters and return types
  • Write single-expression functions
  • Understand Unit return type
  • Call functions from main

Declaring a Function

Use fun to declare a function. Specify parameter types and the return type after : .

Basic Function

kotlin
fun greet() {
    println("Hello from KotlinGuide!")
}

fun main() {
    greet()
    greet()
}
Output
Hello from KotlinGuide! Hello from KotlinGuide!

fun greet() declares a function with no parameters and no return value. Calling greet() twice prints the message twice.

Beginner Tip: Functions must be declared before or after main — not inside it. Top-level functions can be declared anywhere in the file.

Parameters and Return Types

Declare parameters as name: Type. The return type follows the closing parenthesis.

Parameters and Return

kotlin
fun add(a: Int, b: Int): Int {
    return a + b
}

fun describe(name: String, age: Int): String {
    return "$name is $age years old"
}

fun main() {
    println(add(5, 3))
    println(describe("Juned", 25))
}
Output
8 Juned is 25 years old

Parameters use name: Type syntax. The return type Int tells the compiler what this function produces. return sends the value back to the caller.

Single-Expression Functions

When a function body is a single expression, use = instead of {} and return.

Single-Expression Function

kotlin
fun square(n: Int): Int = n * n
fun greet(name: String) = "Hello, $name!"
fun isEven(n: Int) = n % 2 == 0

fun main() {
    println(square(5))
    println(greet("Kotlin"))
    println(isEven(4))
}
Output
25 Hello, Kotlin! true

= replaces { return ... }. The return type can be omitted when it can be inferred from the expression.

Unit Return Type

Functions that return nothing have return type Unit (similar to void in Java). Unit can be omitted.

Unit Return

kotlin
fun printLine(msg: String): Unit {
    println(msg)
}

// Same — Unit is optional
fun printLine2(msg: String) {
    println(msg)
}

fun main() {
    printLine("Explicit Unit")
    printLine2("Implicit Unit")
}
Output
Explicit Unit Implicit Unit

Unit is the return type for functions that do not return a meaningful value. The compiler infers it automatically when omitted.

Practice Exercise

Exercisepredict output

What prints? fun multiply(x: Int, y: Int) = x * y fun main() { println(multiply(4, 5)) }

Quick Quiz

Quick Quiz

Which syntax correctly declares a Kotlin function that returns an Int?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-01Kotlin 2.0

Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review