Bird
0
0

Examine the Swift code below and identify the error:

medium📝 Debug Q6 of 15
Swift - Functions
Examine the Swift code below and identify the error:
func container() {
  innerFunc()
  func innerFunc() {
    print("Hello")
  }
}
container()
AinnerFunc() is not accessible outside container(), causing an error.
BCalling innerFunc() before its declaration causes a compile-time error.
CMissing return type for innerFunc() causes an error.
DThe print statement inside innerFunc() is invalid syntax.
Step-by-Step Solution
Solution:
  1. Step 1: Understand function declaration order

    In Swift, nested functions must be declared before they are called.
  2. Step 2: Analyze the code

    innerFunc() is called before its declaration inside container(), which is invalid.
  3. Final Answer:

    Calling innerFunc() before its declaration causes a compile-time error. -> Option B
  4. Quick Check:

    Nested functions must be declared before use. [OK]
Quick Trick: Declare nested functions before calling them inside outer function [OK]
Common Mistakes:
  • Calling nested function before declaration
  • Assuming nested functions are accessible globally
  • Confusing syntax errors with logical errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes