Bird
0
0

Identify the error in this Swift code using nested functions:

medium📝 Debug Q14 of 15
Swift - Functions
Identify the error in this Swift code using nested functions:
func calculate() {
    innerFunc() 
    func innerFunc() {
        print("Inside inner")
    }
}
calculate()
ANo error, code runs fine
BMissing return type for innerFunc
CinnerFunc cannot be nested inside calculate
DinnerFunc() called before its definition causes error
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes