Bird
Raised Fist0
Swiftprogramming~20 mins

Nested functions in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested function call
What is the output of this Swift code with nested functions?
Swift
func outer() -> String {
    func inner() -> String {
        return "Hello"
    }
    return inner() + " World"
}
print(outer())
AHello World
BWorld
CHello
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the inner function is called and combined with a string.
❓ Predict Output
intermediate
2:00remaining
Value returned by nested function with parameter
What value does the function call outer(3) return in this Swift code?
Swift
func outer(_ x: Int) -> Int {
    func inner(_ y: Int) -> Int {
        return y * 2
    }
    return inner(x) + 1
}
let result = outer(3)
A7
B6
C8
DCompilation error
Attempts:
2 left
💡 Hint
Calculate inner(3) first, then add 1.
🔧 Debug
advanced
2:00remaining
Identify the error in nested function usage
What error does this Swift code produce when compiled?
Swift
func outer() {
    func inner() {
        print("Inside inner")
    }
}
inner()
AError: Missing return in function expected to return value
BError: Use of unresolved identifier 'inner'
CNo error, prints "Inside inner"
DError: Function 'outer' must be called before 'inner'
Attempts:
2 left
💡 Hint
Check the scope of the inner function.
❓ Predict Output
advanced
2:00remaining
Output of nested function modifying outer variable
What is printed by this Swift code?
Swift
func outer() -> Int {
    var count = 0
    func inner() {
        count += 1
    }
    inner()
    inner()
    return count
}
print(outer())
ACompilation error
B0
C1
D2
Attempts:
2 left
💡 Hint
Consider how many times inner() changes count.
🧠 Conceptual
expert
3:00remaining
Behavior of nested functions and variable capture
Consider this Swift code. What is the output when calling outer()?
Swift
func outer() -> () -> Int {
    var x = 0
    func inner() -> Int {
        x += 1
        return x
    }
    return inner
}
let f = outer()
print(f())
print(f())
A
0
0
B
1
1
C
1
2
DCompilation error
Attempts:
2 left
💡 Hint
Think about how the inner function remembers the variable x.

Practice

(1/5)
1. What is a nested function in Swift?
easy
A. A function that calls itself
B. A function that returns another function
C. A function defined inside another function
D. A function defined outside any class or struct

Solution

  1. Step 1: Understand the definition of nested functions

    Nested functions are functions written inside other functions to organize code better.
  2. Step 2: Match the definition to the options

    A function defined inside another function correctly describes a function inside another function, which is the definition of nested functions.
  3. Final Answer:

    A function defined inside another function -> Option C
  4. Quick Check:

    Nested function = function inside function [OK]
Hint: Nested means inside another function [OK]
Common Mistakes:
  • Confusing nested with recursive functions
  • Thinking nested functions are global
  • Mixing nested with returning functions
2. Which of the following is the correct syntax to define a nested function in Swift?
easy
A. func outer() { func inner() { } }
B. func outer() { func inner; }
C. func outer() { inner func() { } }
D. func outer() { func inner() }

Solution

  1. Step 1: Review Swift function syntax

    Functions are defined with 'func name() { }'. Nested functions follow the same syntax inside another function.
  2. Step 2: Check each option for correct syntax

    func outer() { func inner() { } } uses correct syntax with braces for both outer and inner functions. Others have missing braces or wrong order.
  3. Final Answer:

    func outer() { func inner() { } } -> Option A
  4. Quick Check:

    Nested function syntax = func inside func with braces [OK]
Hint: Look for 'func' with braces inside another function [OK]
Common Mistakes:
  • Omitting braces for inner function
  • Using semicolon instead of braces
  • Placing 'func' keyword incorrectly
3. What will be the output of the following Swift code?
func greet() {
    func sayHello() {
        print("Hello")
    }
    sayHello()
}
greet()
medium
A. Hello
B. No output
C. Error: sayHello not found
D. HelloHello

Solution

  1. Step 1: Understand function calls inside nested functions

    The outer function 'greet' defines 'sayHello' inside it and calls it once.
  2. Step 2: Trace the execution

    Calling 'greet()' runs 'sayHello()', which prints "Hello" once.
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    Nested call prints once = Hello [OK]
Hint: Inner function called once inside outer function [OK]
Common Mistakes:
  • Thinking inner function runs twice
  • Assuming inner function is global
  • Expecting no output because inner function is nested
4. Identify the error in this Swift code using nested functions:
func calculate() {
    innerFunc() 
    func innerFunc() {
        print("Inside inner")
    }
}
calculate()
medium
A. No error, code runs fine
B. Missing return type for innerFunc
C. innerFunc cannot be nested inside calculate
D. innerFunc() called before its definition causes error

Solution

  1. Step 1: Analyze function call order

    The code calls 'innerFunc()' before it is defined inside 'calculate()'. Swift requires functions to be defined before use.
  2. Step 2: Understand Swift's function declaration rules

    Calling a nested function before its declaration causes a compile-time error.
  3. Final Answer:

    innerFunc() called before its definition causes error -> Option D
  4. Quick Check:

    Call before definition = error [OK]
Hint: Define nested functions before calling them [OK]
Common Mistakes:
  • Assuming nested functions can be called before definition
  • Thinking missing return type causes error here
  • Believing nested functions can't be defined inside
5. How can you use nested functions to create a function that returns another function which adds a fixed number to its input? Choose the correct Swift code.
hard
A. func makeAdder(_ x: Int) -> Int { func adder(y: Int) -> Int { return x + y } return adder(5) }
B. func makeAdder(_ x: Int) -> (Int) -> Int { func adder(y: Int) -> Int { return x + y } return adder }
C. func makeAdder(_ x: Int) -> (Int) -> Int { return func(y: Int) -> Int { x + y } }
D. func makeAdder(_ x: Int) -> (Int) -> Int { func adder(y: Int) -> Int { return y } return adder }

Solution

  1. Step 1: Understand the goal

    We want a function that returns another function adding a fixed number 'x' to input 'y'.
  2. Step 2: Check each option's logic

    func makeAdder(_ x: Int) -> (Int) -> Int { func adder(y: Int) -> Int { return x + y } return adder } defines nested 'adder' using 'x' and returns it correctly. func makeAdder(_ x: Int) -> Int { func adder(y: Int) -> Int { return x + y } return adder(5) } returns a value, not a function. func makeAdder(_ x: Int) -> (Int) -> Int { return func(y: Int) -> Int { x + y } } has invalid syntax. func makeAdder(_ x: Int) -> (Int) -> Int { func adder(y: Int) -> Int { return y } return adder } returns a function ignoring 'x'.
  3. Final Answer:

    func makeAdder(_ x: Int) -> (Int) -> Int { func adder(y: Int) -> Int { return x + y } return adder } -> Option B
  4. Quick Check:

    Nested function returns function adding x = func makeAdder(_ x: Int) -> (Int) -> Int { func adder(y: Int) -> Int { return x + y } return adder } [OK]
Hint: Return nested function that uses outer parameter [OK]
Common Mistakes:
  • Returning a value instead of a function
  • Incorrect nested function syntax
  • Ignoring outer function parameter inside nested function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard