Bird
0
0

How do you correctly store a function named displayMessage into a variable called show in Swift?

easy📝 Syntax Q3 of 15
Swift - Functions
How do you correctly store a function named displayMessage into a variable called show in Swift?
Avar show = displayMessage() -> Void
Blet show = displayMessage()
Cvar show = displayMessage()
Dlet show = displayMessage
Step-by-Step Solution
Solution:
  1. Step 1: Understand function assignment

    Assigning a function to a variable means referencing the function itself, not calling it.
  2. Step 2: Avoid parentheses

    Using parentheses calls the function immediately, so omit them to assign the function.
  3. Final Answer:

    let show = displayMessage -> Option D
  4. Quick Check:

    Assign function without parentheses [OK]
Quick Trick: Assign function without parentheses to variable [OK]
Common Mistakes:
  • Using parentheses which calls the function immediately
  • Declaring variable with incorrect type
  • Trying to assign function call result instead of function reference

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes