What does it mean to return a function from another function in Kotlin?
It means a function can produce another function as its result. This returned function can then be called later, like a recipe that gives you another recipe.
Click to reveal answer
intermediate
How do you declare a function in Kotlin that returns another function?
You specify the return type as a function type. For example, fun outer(): (Int) -> Int means outer returns a function that takes an Int and returns an Int.
Click to reveal answer
beginner
What is a practical example of returning a function from a function?
Creating a multiplier function: a function that returns another function which multiplies input by a fixed number. This helps reuse logic with different multipliers.
Click to reveal answer
intermediate
Can the returned function capture variables from the outer function?
Yes! The returned function can remember variables from the outer function, like a backpack carrying important things for later use. This is called a closure.
Click to reveal answer
beginner
Write a simple Kotlin function that returns a function which adds 5 to its input.
fun addFive(): (Int) -> Int = { x -> x + 5 }
Click to reveal answer
What is the return type of this Kotlin function? fun makeAdder(): (Int) -> Int
AA function that takes an Int and returns an Int
BAn Int value
CA String
DA function that takes no arguments
✗ Incorrect
The return type (Int) -> Int means the function returns another function that takes an Int and returns an Int.
What keyword is used to define a lambda expression in Kotlin?
Areturn
Blambda
Cfun
D{} (curly braces)
✗ Incorrect
Lambdas in Kotlin are defined using curly braces {} with parameters and body inside.
What is a closure in Kotlin functions?
AA function that closes the program
BA function that returns nothing
CA function that captures variables from its surrounding scope
DA function that takes no parameters
✗ Incorrect
A closure is a function that remembers variables from where it was created.
Which of these is a correct way to return a function from a Kotlin function?
Afun outer() -> Int { return 2 }
Bfun outer() = { x: Int -> x * 2 }
Cfun outer(): Int = 5
Dfun outer() { return x * 2 }
✗ Incorrect
Option B returns a lambda function that doubles its input.
If a function returns another function, how do you call the inner function?
ACall the outer function and then call the result with parentheses
BCall only the outer function
CCall only the inner function directly
DYou cannot call the inner function
✗ Incorrect
You first call the outer function to get the returned function, then call that function with arguments.
Explain how returning functions from functions works in Kotlin and why it might be useful.
Think about functions as recipes that can give you other recipes.
You got /4 concepts.
Write a Kotlin function that returns a function which adds a given number to its input.
Use a closure to remember the number to add.
You got /3 concepts.
Practice
(1/5)
1. What does it mean when a Kotlin function returns another function?
easy
A. The function can only return primitive data types like Int or String.
B. The function cannot have parameters if it returns another function.
C. The outer function produces a function as its result that can be called later.
D. The returned function must be called immediately inside the outer function.
Solution
Step 1: Understand function return types
In Kotlin, a function can return any type, including another function type.
Step 2: Interpret returning a function
Returning a function means the outer function gives back a function that can be stored or called later.
Final Answer:
The outer function produces a function as its result that can be called later. -> Option C
Quick Check:
Returning a function means output is a callable function [OK]
Hint: Returning function means output is a callable function [OK]
Common Mistakes:
Thinking only primitive types can be returned
Assuming returned function must be called immediately
Believing returned functions cannot have parameters
2. Which of the following is the correct syntax to declare a Kotlin function that returns another function taking an Int and returning a String?
easy
A. fun outer(): (Int) -> String = { i -> i.toString }
B. fun outer(): (Int) -> String { return { i -> i.toString() } }
C. fun outer(): (Int) String { return { i -> i.toString() } }
D. fun outer(): Int -> String = return { i -> i.toString() } }
Solution
Step 1: Check function return type syntax
In Kotlin, a function returning another function uses syntax like: fun name(): (ParamType) -> ReturnType
Step 2: Verify the return statement
The function returns a lambda matching (Int) -> String, e.g., { i -> i.toString() }
Final Answer:
fun outer(): (Int) -> String { return { i -> i.toString() } } -> Option B
Quick Check:
Correct function type syntax = fun outer(): (Int) -> String { return { i -> i.toString() } } [OK]
Hint: Return type for function is (ParamType) -> ReturnType [OK]
Common Mistakes:
Using arrow -> incorrectly in return type
Mixing return type syntax with function body
Omitting parentheses around function type
3. What is the output of this Kotlin code?
fun makeMultiplier(x: Int): (Int) -> Int {
return { y -> x * y }
}
fun main() {
val multiplyBy3 = makeMultiplier(3)
println(multiplyBy3(5))
}
medium
A. 35
B. 8
C. Compilation error
D. 15
Solution
Step 1: Understand makeMultiplier function
makeMultiplier returns a function that multiplies its input by x (captured from outer function).
Step 2: Trace main function calls
multiplyBy3 is a function multiplying by 3; multiplyBy3(5) = 3 * 5 = 15.
Final Answer:
15 -> Option D
Quick Check:
3 * 5 = 15 [OK]
Hint: Returned function uses captured outer variable [OK]
Common Mistakes:
Confusing parameter names
Forgetting the closure captures x
Expecting immediate multiplication without calling returned function
4. Identify the error in this Kotlin code that tries to return a function:
fun outer(): (Int) -> Int {
val inner = { x: Int -> x * 2 }
return inner()
}
medium
A. Calling inner() instead of returning the function itself
B. Missing return type declaration for outer function
C. Incorrect lambda parameter syntax
D. Cannot return a lambda from a function
Solution
Step 1: Analyze return statement
inner is a lambda function; inner() calls it and returns Int, not a function.
Step 2: Correct return value
To return the function itself, return inner without parentheses.
Final Answer:
Calling inner() instead of returning the function itself -> Option A
Quick Check:
Return function, not function call [OK]
Hint: Return function name without () to return function itself [OK]
Common Mistakes:
Returning function call instead of function
Omitting function return type
Misunderstanding lambda syntax
5. Given this Kotlin code, what will be the output?
fun createCounter(start: Int): () -> Int {
var count = start
return {
count += 1
count
}
}
fun main() {
val counter = createCounter(10)
println(counter())
println(counter())
}
hard
A. 11
12
B. 10
11
C. 12
13
D. Compilation error
Solution
Step 1: Understand closure capturing variable
The returned lambda captures 'count' variable and increments it each call.
Step 2: Trace calls to counter()
First call: count = 10 + 1 = 11; prints 11. Second call: count = 11 + 1 = 12; prints 12.
Final Answer:
11
12 -> Option A
Quick Check:
Closure increments count on each call [OK]
Hint: Closures keep state between calls [OK]
Common Mistakes:
Assuming count resets each call
Confusing initial value with first output
Expecting compilation error due to var usage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.