← Back to Kotlin Course | Chapter 4: Functions | Lesson 1 of 7

Function Basics

A function is a named recipe of steps that you can reuse any time you need the computer to do that same job again.
Syntax
kotlin
fun functionName(param: Type): ReturnType {
    return value
}

fun functionName(param: Type) {   // no return value
}

Declaring a Function

A function starts with fun, followed by its name, a parenthesized parameter list, an optional return type, and a body in curly braces.

Example: Declaring a Function

markup
// Define the function `greet` taking `name` and returning a String
fun greet(name: String): String {
    // Return `"Hello, $name!"` from this function
    return "Hello, $name!"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `greet("Kotlin")` to the console, with a trailing newline
    println(greet("Kotlin"))
}

Functions with No Return Value

A function that only performs an action, like printing, and produces no useful value has a return type of Unit, which can be omitted since it is the default.

Example: Functions with No Return Value

markup
// Define the function `printBanner` taking `text`
fun printBanner(text: String) {
    // Print "=== $text ===" to the console, with a trailing newline
    println("=== $text ===")
}

// Entry point: execution of the program starts here
fun main() {
    printBanner("Welcome")
}

Multiple Parameters

A function can take several typed parameters separated by commas, and every one of them (unless it has a default) must be supplied at the call site.

Example: Multiple Parameters

markup
// Define the function `add` taking `a`, `b` and returning an Int
fun add(a: Int, b: Int): Int {
    // Return `a + b` from this function
    return a + b
}

// Entry point: execution of the program starts here
fun main() {
    // Print "Sum: ${add(3, 4)}" to the console, with a trailing newline
    println("Sum: ${add(3, 4)}")
}

Calling Functions

Calling a function uses its name followed by parentheses containing arguments in the same order as the parameters were declared.

Example: Calling Functions

markup
// Define the function `multiply` taking `a`, `b` and returning an Int
fun multiply(a: Int, b: Int): Int {
    // Return `a * b` from this function
    return a * b
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `result`, set to `multiply(6, 7)`
    val result = multiply(6, 7)
    // Print "6 x 7 = $result" to the console, with a trailing newline
    println("6 x 7 = $result")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to declare a return type when a function does not implicitly return Unit, causing a compiler error.
  2. Confusing parameter order when calling a function positionally, sending values to the wrong parameters.
  3. Not realizing a function with no explicit return and a block body implicitly returns Unit, not the last expression's value.
Chapter Summary
  • Functions are declared with fun name(parameters): ReturnType { body }.
  • A function with no meaningful return value has (or can omit) a return type of Unit.
  • Parameters are typed and, unless given defaults, must all be supplied when calling the function.
  • The return keyword sends a value back from a block-bodied function.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.