0
0
Kotlinprogramming~5 mins

Returning functions from functions in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
What keyword is used to define a lambda expression in Kotlin?
Areturn
Blambda
Cfun
D{} (curly braces)
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
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 }
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
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.