Function declaration syntax in Kotlin - Time & Space Complexity
We want to understand how the time it takes to run a function changes as the input changes.
Specifically, we ask: how does the function's work grow when input size grows?
Analyze the time complexity of the following code snippet.
fun printNumbers(n: Int) {
for (i in 1..n) {
println(i)
}
}
This function prints numbers from 1 up to n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints each number.
- How many times: It runs exactly n times, once for each number.
As n grows, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print actions |
| 100 | 100 print actions |
| 1000 | 1000 print actions |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the function grows in a straight line with the input size.
[X] Wrong: "The function runs in constant time because it just prints numbers."
[OK] Correct: Printing happens once for each number, so more numbers mean more work.
Understanding how loops affect time helps you explain your code clearly and shows you know how programs grow with input.
"What if we changed the function to print only even numbers up to n? How would the time complexity change?"
Practice
greet in Kotlin that takes no parameters and returns no value?Solution
Step 1: Identify the correct keyword for function declaration
Kotlin uses the keywordfunto declare functions. Options usingfunctionordefare incorrect.Step 2: Check the function syntax for parameters and braces
The function must have parentheses even if there are no parameters, so fun greet { println("Hello") } is incorrect because it misses parentheses.Final Answer:
fun greet() { println("Hello") } -> Option BQuick Check:
fun + name + () + body = B [OK]
- Using 'function' or 'def' instead of 'fun'
- Omitting parentheses for parameter list
- Missing curly braces for function body
add that takes two Int parameters and returns their sum as an Int?Solution
Step 1: Check parameter declaration syntax
Parameters must be declared asname: Type, so fun add(a Int, b Int): Int { return a + b } is incorrect because it misses colons.Step 2: Verify return type and function body syntax
fun add(a: Int, b: Int): Int = a + b uses the correct syntax with an expression body and return type. fun add(a: Int, b: Int) -> Int { return a + b } uses '->' which is invalid in Kotlin. fun add(a: Int, b: Int): Int { a + b } misses the return keyword in a block body, so it won't return the sum.Final Answer:
fun add(a: Int, b: Int): Int = a + b -> Option DQuick Check:
fun + parameters with colons + : ReturnType + = expression = A [OK]
- Omitting colons between parameter names and types
- Using '->' instead of ':' for return type
- Missing 'return' in block body functions
fun multiply(x: Int, y: Int): Int {
return x * y
}
fun main() {
println(multiply(3, 4))
}Solution
Step 1: Understand the multiply function
The function takes two integers and returns their product usingreturn x * y.Step 2: Calculate the function call multiply(3, 4)
3 multiplied by 4 equals 12, so the function returns 12.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Assuming syntax error due to missing expression body
fun divide(a: Int, b: Int): Int {
return a / b
Solution
Step 1: Check function syntax completeness
The function body starts with '{' but there is no closing '}', so the code is incomplete.Step 2: Verify other syntax parts
The return type is correct, parameters have types, and the function name is valid, so no other errors.Final Answer:
Missing closing brace '}' -> Option AQuick Check:
All blocks need matching braces [OK]
- Forgetting to close function body with '}'
- Misplacing colons in parameters
- Using invalid function names
formatName that takes two parameters: firstName and lastName (both Strings), and returns a single String in the format "Last, First". Which of the following is the correct declaration and implementation?Solution
Step 1: Check parameter and return type syntax
Parameters must usename: Typeand return type must match. A uses: Intbut returns String (type mismatch). B misses colons (firstName Stringinvalid).Step 2: Verify function body syntax
C has block body{ "$lastName, $firstName" }withoutreturn, returns Unit not String. D correctly uses block body withreturnand string interpolation.Final Answer:
fun formatName(firstName: String, lastName: String): String { return "$lastName, $firstName" } -> Option CQuick Check:
Correct types + block body with return + interpolation = D [OK]
- Omitting return keyword in block body
- Missing return type declaration
- Incorrect string concatenation or interpolation
Ready to prove it?
Pass the test and this pattern moves unknown
