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):

Kotlin

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

Kotlin

Call it:

Kotlin

2. Parameters with Default Values

Kotlin lets you give default values to parameters — just like Python!

Kotlin

Calling it in different ways

Kotlin

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 =

Kotlin

Even shorter (type inference)

Kotlin

With default values

Kotlin

Real-life example – calculate square

Kotlin

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)

Kotlin

Explicit Unit (rarely needed)

Kotlin

Fun fact: You can even write return Unit — but nobody does it! Kotlin automatically returns Unit if no value is returned.

Kotlin

5. Putting It All Together – Real-World Example

Let’s create a small greeting function with defaults, named arguments, and single-expression style.

Kotlin

Calling it in many ways

Kotlin

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!)

  1. Basic Create a function fun welcome(name: String = “Guest”) that prints “स्वागत आहे $name!”
  2. Medium Write a function fun calculateTotal(price: Double, discountPercent: Double = 0.0) that returns price after discount.
  3. Advanced Create a function fun introduce(name: String, age: Int = 20, city: String = “Mumbai”) → return a formatted string using templates.
  4. Fun Make a function fun printTicket(movie: String = “RRR”, seat: String = “A12”, time: String = “7:00 PM”) that prints a nice ticket.
  5. 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!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *