Functions.
We know how to store data in variables. But programs do more than store. They compute. They transform. They act. Functions are how we organize these actions. Today we learn to define and use functions in Kotlin.
A function is a named block of code that performs a task.
You give it inputs. It does something. It may produce an output.
Functions let you reuse code. Write the logic once. Call it many times. No copying, no duplication.
Functions organize code. Instead of one giant sequence, you have named pieces. Each piece has a purpose. The code becomes readable.
Functions are the building blocks of programs. Small functions combine into larger capabilities.
In Kotlin, you declare a function with the fun keyword.
fun followed by the name, parentheses with parameters, optional return type, and a body.
A simple function: fun greet opening parenthesis closing parenthesis opening brace println "Hello" closing brace. This function takes no inputs and produces no output. It just prints.
A function with a parameter: fun greet with name of type String. The parameter name has type String. When you call the function, you provide a value for name.
A function with a return value: fun add with a of type Int and b of type Int, returning Int. The return type is Int, specified after the parameters with a colon. The return keyword sends the result back to the caller.
The structure is: fun name parentheses parameters colon ReturnType braces body.
Parameters are inputs to a function.
Each parameter has a name and a type. The type is required. The compiler needs to know what kind of data to expect.
Multiple parameters are separated by commas: fun add with a Int and b Int.
Inside the function, parameters are like val variables. You can read them. You cannot reassign them.
When you call the function, you provide arguments. These are the actual values for the parameters. add with 5 and 3 passes 5 for a and 3 for b.
Parameters are how functions become flexible. The same function works with different inputs.
A function can produce a result. This is the return value.
The return type is declared after the parameters: fun square with n Int returning Int.
Inside the function, use return to send the value back: return n times n.
When a function returns a value, you can use it in expressions: val result equals square of 5.
If a function does not return a useful value, its return type is Unit. Unit means no meaningful return. You can omit it: fun greet is the same as fun greet returning Unit.
Every function has a return type. Either a meaningful value or Unit.
Many functions have a simple body: just one expression.
Kotlin has a short form for these: fun square with n Int returning Int equals n times n.
No braces. No return keyword. Just equals and the expression.
The type can often be inferred: fun square with n Int equals n times n. The compiler knows the result of n times n is Int.
Single-expression functions are concise. They show the essence: input, transformation, output. No ceremony.
Use them for simple computations. For complex logic with multiple statements, use the full block form.
Sometimes parameters have sensible defaults.
In Kotlin, you can specify default values: fun greet with name String equals "World".
If the caller provides a value, it is used. If not, the default is used.
greet with "Alice" prints "Hello, Alice". greet with no arguments prints "Hello, World".
Defaults reduce overloading. In Java, you might write three versions of a function for different parameter combinations. In Kotlin, one function with defaults handles all cases.
Place parameters with defaults at the end. This allows callers to omit them positionally.
When calling a function, you can name the arguments.
Instead of add 5, 3, you write add a equals 5, b equals 3.
Named arguments make code clearer. greet name equals "Alice", greeting equals "Hi" is more readable than greet "Alice", "Hi".
Named arguments allow any order. createUser email equals "a@b.com", name equals "Alice" works even if name is the first parameter.
Named arguments are especially useful with many parameters or default values. You can skip defaults: configure debug equals true sets only what you need.
Use named arguments when it improves clarity.
To call a function, write its name followed by arguments in parentheses.
greet parentheses calls the greet function with no arguments.
add 5, 3 calls add with arguments 5 and 3.
The function executes. If it returns a value, you can use it: val sum equals add 5, 3.
Functions can call other functions. A function can be built from smaller functions.
The call stack tracks which function is active. When one function calls another, the first pauses, the second runs, then the first resumes.
Let us summarize.
Functions are named blocks of code that perform tasks.
Declare with fun, name, parameters, optional return type, and body.
Parameters are typed inputs. Return type specifies what comes out.
Single-expression functions use equals for concise syntax.
Default parameter values reduce the need for overloads.
Named arguments improve clarity at call sites.
Functions organize and reuse code. They are essential to well-structured programs.