Chapter 8: Functions – Part 1 (Basics)
Functions – Part 1 (Basics) — this is the chapter where your Kotlin code starts becoming beautiful, concise, and super readable! ☕✨
Functions in Kotlin are much cleaner than in Java — fewer keywords, optional types, default values, named arguments, single-expression functions… it feels like magic once you get used to it.
Imagine we’re sitting together in a cozy Bandra café — I’m going to explain everything super slowly, step by step, with lots of real-life analogies, complete runnable examples, clear breakdowns, tables, common mistakes with fixes, and fun facts so everything sticks perfectly.
Let’s begin!
1. Function Declaration – Basic Syntax
Syntax (the most common form):
|
0 1 2 3 4 5 6 7 8 9 |
fun functionName(parameter1: Type, parameter2: Type): ReturnType { // function body return something } |
Key points:
- fun keyword is mandatory
- Parameters are written as name: Type (reverse of Java)
- Return type is after parameters with :
- Return type is optional if Kotlin can infer it (especially with single-expression functions)
- return is optional in many cases
Simplest function
|
0 1 2 3 4 5 6 7 8 |
fun sayHello() { println("नमस्ते Webliance! स्वागत आहे Kotlin मध्ये!") } |
Call it:
|
0 1 2 3 4 5 6 7 8 |
fun main() { sayHello() // Prints: नमस्ते Webliance! स्वागत आहे Kotlin मध्ये! } |
2. Parameters with Default Values
Kotlin lets you give default values to parameters — just like Python!
|
0 1 2 3 4 5 6 7 8 |
fun greet(name: String = "Guest", greeting: String = "नमस्ते") { println("$greeting $name!") } |
Calling it in different ways
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
fun main() { greet() // नमस्ते Guest! greet("Amit") // नमस्ते Amit! greet("Priya", "हाय") // हाय Priya! greet(greeting = "हॅलो") // हॅलो Guest! (named argument) greet(name = "Rahul", greeting = "हॅलो") // हॅलो Rahul! } |
Named arguments — you can specify parameter names → order doesn’t matter!
3. Single-Expression Functions (=)
If your function has only one expression, you can skip {} and return — use =
|
0 1 2 3 4 5 6 |
fun add(a: Int, b: Int): Int = a + b |
Even shorter (type inference)
|
0 1 2 3 4 5 6 |
fun add(a: Int, b: Int) = a + b // Kotlin knows it returns Int |
With default values
|
0 1 2 3 4 5 6 |
fun greet(name: String = "Guest") = "नमस्ते $name!" |
Real-life example – calculate square
|
0 1 2 3 4 5 6 7 8 9 10 11 |
fun square(number: Int) = number * number fun main() { println(square(5)) // 25 println(square(12)) // 144 } |
4. Unit Return Type (Like void in Java)
Unit is Kotlin’s equivalent of void — but it’s actually an object (singleton).
Implicit Unit (most common – you don’t write it)
|
0 1 2 3 4 5 6 7 8 |
fun sayHi() { println("Hi!") } |
Explicit Unit (rarely needed)
|
0 1 2 3 4 5 6 7 8 |
fun sayHi(): Unit { println("Hi!") } |
Fun fact: You can even write return Unit — but nobody does it! Kotlin automatically returns Unit if no value is returned.
|
0 1 2 3 4 5 6 |
fun doNothing(): Unit = Unit // Valid, but silly |
5. Putting It All Together – Real-World Example
Let’s create a small greeting function with defaults, named arguments, and single-expression style.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
fun greetUser( name: String = "Guest", age: Int? = null, city: String = "Mumbai", greeting: String = "नमस्ते" ): String = buildString { append("$greeting $name!") if (age != null) { append(" तुम्ही $age वर्षांचे आहात.") } append(" $city मधून बोलत आहात का?") } |
Calling it in many ways
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
fun main() { // 1. All defaults println(greetUser()) // नमस्ते Guest! Mumbai मधून बोलत आहात का? // 2. Only name println(greetUser("Amit")) // नमस्ते Amit! Mumbai मधून बोलत आहात का? // 3. Name + age (named arguments) println(greetUser(name = "Priya", age = 24)) // नमस्ते Priya! तुम्ही 24 वर्षांचे आहात. Mumbai मधून बोलत आहात का? // 4. All parameters (different order!) println(greetUser(city = "Pune", name = "Rahul", age = 22, greeting = "हाय")) // हाय Rahul! तुम्ही 22 वर्षांचे आहात. Pune मधून बोलत आहात का? } |
6. Quick Recap Table (Your Cheat Sheet)
| Feature | Kotlin Way (Best Practice) | Java Equivalent (for comparison) |
|---|---|---|
| Basic function | fun sayHello() { … } | void sayHello() { … } |
| With parameters | fun add(a: Int, b: Int) = a + b | int add(int a, int b) { return a + b; } |
| Default parameters | fun greet(name: String = “Guest”) | No direct equivalent |
| Named arguments | greet(age = 25, name = “Amit”) | No direct equivalent |
| Single-expression | fun square(n: Int) = n * n | int square(int n) { return n * n; } |
| Unit return | fun log() { println(“Log”) } (implicit) | void log() { … } |
7. Common Newbie Mistakes & Fixes
| Mistake | Problem | Fix |
|---|---|---|
| Forgetting : Type after parameter | Compile error | Always write name: String |
| Writing return Unit everywhere | Looks like Java – not idiomatic | Just skip return type – Kotlin infers Unit |
| Using var for function parameters | Parameters are always val | Use val or nothing (implicit val) |
| Forgetting to use named arguments | Hard to read when many parameters | Use names: greet(age = 25, name = “Amit”) |
| Not using single-expression = | Verbose code | Use = when function has one expression |
8. Homework for You (Let’s Make It Fun!)
- Basic Create a function fun welcome(name: String = “Guest”) that prints “स्वागत आहे $name!”
- Medium Write a function fun calculateTotal(price: Double, discountPercent: Double = 0.0) that returns price after discount.
- Advanced Create a function fun introduce(name: String, age: Int = 20, city: String = “Mumbai”) → return a formatted string using templates.
- Fun Make a function fun printTicket(movie: String = “RRR”, seat: String = “A12”, time: String = “7:00 PM”) that prints a nice ticket.
- Challenge Create a function fun max(a: Int, b: Int, c: Int = 0) that returns the maximum of three numbers using single-expression style.
You’ve just unlocked Kotlin’s beautiful function syntax — now your code is clean, concise, and super readable!
