0
0
Kotlinprogramming~10 mins

Parameters with default values in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parameters with default values
Function call
Check if argument provided?
NoUse default value
Yes
Assign argument to parameter
Execute function body
Return result or finish
When a function is called, Kotlin checks if each parameter has an argument. If not, it uses the default value. Then it runs the function with those values.
Execution Sample
Kotlin
fun greet(name: String = "Friend") {
    println("Hello, $name!")
}

greet()
greet("Alice")
This code defines a function with a default parameter and calls it once without and once with an argument.
Execution Table
StepFunction CallParameter 'name'ActionOutput
1greet()No argumentUse default "Friend"Hello, Friend!
2greet("Alice")Argument "Alice"Use argumentHello, Alice!
3End--No more calls
💡 All function calls completed
Variable Tracker
VariableStartAfter call 1After call 2Final
name-"Friend""Alice"-
Key Moments - 2 Insights
Why does greet() print "Hello, Friend!" even though no argument is given?
Because in step 1 of the execution_table, no argument is passed, so Kotlin uses the default value "Friend" for the parameter 'name'.
What happens if we provide an argument like "Alice"?
As shown in step 2, the argument "Alice" replaces the default, so the function prints "Hello, Alice!".
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' during the first function call?
Anull
B"Alice"
C"Friend"
DNo value
💡 Hint
Check the 'Parameter name' column in row 1 of the execution_table.
At which step does the function use the argument instead of the default?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Action' column in the execution_table for step 2.
If we call greet("Bob"), what will be printed?
AHello, Bob!
BHello, Alice!
CHello, Friend!
DError
💡 Hint
Based on the pattern in the execution_table, providing an argument replaces the default.
Concept Snapshot
fun functionName(param: Type = defaultValue) {
  // function body
}

- If argument missing, defaultValue is used.
- If argument given, it overrides default.
- Helps avoid overloads and simplifies calls.
Full Transcript
This visual trace shows how Kotlin functions handle parameters with default values. When the function greet is called without an argument, Kotlin uses the default value "Friend" for the parameter 'name' and prints "Hello, Friend!". When called with an argument like "Alice", it uses that argument instead and prints "Hello, Alice!". This feature lets you write simpler functions that work with or without some arguments.