Functions
Kotlin is a modern, concise language where functions are first-class building blocks — declared with fun , they package reusable logic and return values cleanly.
Learn Functions in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll define functions with typed parameters and return types, write compact single-expression functions, and understand Unit .
What You'll Learn in This Lesson
1️⃣ Declaring Functions
A Kotlin function starts with fun , followed by a name, parameters written as name: Type , and an optional return type after a colon. Inside, return hands a value back to the caller.
The greet function returns nothing useful, so its type is Unit — Kotlin's equivalent of void . You can omit : Unit entirely.
2️⃣ Single-Expression Functions
When a function is just one expression, you can replace the braces and return with an = sign. The return type is inferred. This is one of the most idiomatic patterns in Kotlin for small, focused functions.
Functions compose naturally: describe(square(2)) first squares 2 to get 4, then describes it. Small functions that each do one thing are the foundation of clean code.
Your turn. Replace each TODO , then run and compare.
Write two small functions — one for the maths, one for the label — and combine them in main() .
📋 Quick Reference — Functions
Practice quiz
Which keyword declares a function in Kotlin?
- func
- def
- fun
- function
Answer: fun. Kotlin functions are declared with the fun keyword.
How is a parameter written in a Kotlin function?
- name: Type
- Type name
- var name
- (Type) name
Answer: name: Type. Parameters are written as name: Type, with the type after the name.
Where does the return type appear in a function signature?
- Before the function name
- After the parameter list, following a colon
- Inside the body only
- It is never written
Answer: After the parameter list, following a colon. The return type comes after the parameter list, following a colon.
What is the return type of a function that returns nothing meaningful?
- Void
- Null
- Nothing
- Unit
Answer: Unit. Such a function returns Unit, Kotlin's equivalent of Java's void.
What is a single-expression function?
- A function with no parameters
- One written with = instead of braces and return, with the type inferred
- A function that returns multiple values
- A function inside a class only
Answer: One written with = instead of braces and return, with the type inferred. When the body is one expression you can use = and drop braces and return; the type is inferred.
In 'fun square(n: Int) = n * n', what is the return type?
- Inferred as Int
- String
- Unit
- Must be declared explicitly or it fails
Answer: Inferred as Int. The return type Int is inferred from the expression n * n.
How is Unit different from Java's void?
- Unit cannot be used at all
- There is no difference in any sense
- Unit is a real type with a single value, fitting the everything-is-an-expression model
- Unit means the function throws
Answer: Unit is a real type with a single value, fitting the everything-is-an-expression model. Unit is a real type with one value, unlike void which is not a value.
What lets you call add(b = 4, a = 3) with order not mattering?
- Default arguments
- Named arguments
- Varargs
- Operator overloading
Answer: Named arguments. Named arguments let you pass arguments by name so order doesn't matter.
Can a non-Unit function skip returning a value on some path?
- Yes, it returns null
- No, it must return on every path or the compiler flags it
- Yes, it returns Unit
- Only inside loops
Answer: No, it must return on every path or the compiler flags it. A non-Unit function must return a value on every path; the compiler enforces this.
What is a clean way to return two related values from a function?
- Use global variables
- Throw them as an exception
- Print them instead
- Return a Pair (or a small data class) and destructure it
Answer: Return a Pair (or a small data class) and destructure it. Return a Pair or small data class, then destructure with val (a, b) = result.