Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Functions
Identify the error in this Swift code:
func greet() {
    print("Hello")
}
let sayHello = greet()
sayHello()
AsayHello is not a function but the result of greet()
Bgreet() cannot be called without parameters
CMissing return type in greet function
DCannot assign function to a constant
Step-by-Step Solution
Solution:
  1. Step 1: Analyze assignment

    'sayHello' is assigned the result of calling 'greet()', which is Void (no value).
  2. Step 2: Calling sayHello()

    Since 'sayHello' is Void, calling 'sayHello()' causes an error.
  3. Final Answer:

    sayHello is not a function but the result of greet() -> Option A
  4. Quick Check:

    Assigning function call result, not function = C [OK]
Quick Trick: Assign function name without () to store function [OK]
Common Mistakes:
  • Calling function when assigning instead of assigning function itself
  • Expecting greet to require parameters
  • Thinking constants cannot hold functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes