Kotlin Functions: Named, Reusable Code Blocks

A function is a named recipe for your code. You give it ingredients (parameters) and it produces a result. They are used everywhere, from calculating values to handling button clicks.
Why it exists
Functions exist to solve the problem of code repetition and disorganization. Instead of writing the same logic multiple times, you can package it into a single, named block that can be called from anywhere. This makes code easier to read, test, and maintain.
The mental model
Think of a function as a specialized machine on an assembly line. It has a clear name describing its job (like doubleValue), defined inputs it accepts (parameters, like a number x), and a specific output it produces (a return value, like 2 * x). You can use this machine over and over without needing to rebuild it each time.
How it works
You declare a function with the fun keyword, followed by its name, parameters in parentheses, and an optional return type. For example, fun powerOf(number: Int, exponent: Int): Int. Inside the function, parameters are read-only. Kotlin offers powerful features like default parameter values, which let you make arguments optional. For example, fun read(off: Int = 0, len: Int = b.size). This avoids creating many overloaded versions of the same function just to skip a parameter.
When to use it
Use functions for any piece of logic you might need more than once. This is a fundamental concept used for almost everything: performing calculations, updating the UI in response to user input, making network requests, or validating data. Grouping logic into functions makes your app's architecture clean and understandable.
When not to use it
There's almost never a time not to use functions to organize logic. However, avoid creating functions that are too large or do too many unrelated things (a "god function"). A function should have a single, clear responsibility. If a function becomes too complex, it's a sign you should break it down into smaller, more focused helper functions.
One canonical example
A common pattern is using default parameters to simplify function calls. Consider this function: fun greet(name: String, message: String = "Hello") { println("message, name!") }. You can call this in two ways. First, providing all arguments: greet("Alice", "Hi") prints "Hi, Alice!". Second, using the default value: greet("Bob") prints "Hello, Bob!". A common footgun is argument order. If the default parameter came first, like fun greet(message: String = "Hello", name: String), you would have to call it with a named argument: greet(name = "Charlie").
Interview question
What is the main purpose of using functions in Kotlin?
- a.To define variables that can be modified from anywhere in the program.
- b.To allow parameters to be mutable by default within their scope.
- c.To automatically optimize code execution speed during compilation.
- d.To package reusable logic, preventing code duplication and enhancing structure.Correct
Why? this is the answer
The card states functions exist "to solve the problem of code repetition and disorganization" by packaging logic into "a single, named block that can be called from anywhere." Option C is incorrect because while good code structure can aid optimization, functions' primary role isn't automatic speed enhancement.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles