Swift - FunctionsWhich of the following is the correct way to assign a function to a variable in Swift?Alet greet = func sayHello() { print("Hello") }Blet greet = sayHelloCfunc greet = sayHello()Dvar greet: sayHello = func()Check Answer
Step-by-Step SolutionSolution:Step 1: Recall Swift syntax for assigning functionsIn Swift, you assign a function by its name without parentheses to a variable or constant.Step 2: Check each option for correct syntaxlet greet = sayHello correctly assigns the function name without calling it. Other options have syntax errors or misuse keywords.Final Answer:let greet = sayHello -> Option BQuick Check:Assign function by name without () = let greet = sayHello [OK]Quick Trick: Assign function by name only, no parentheses [OK]Common Mistakes:Adding parentheses when assigning functionUsing 'func' keyword in assignmentIncorrect variable declaration syntax
Master "Functions" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Collections - Array creation and type inference - Quiz 13medium Control Flow - Guard for early exit pattern - Quiz 2easy Control Flow - Switch statement power - Quiz 14medium Data Types - Tuples for grouped values - Quiz 7medium Data Types - Int, Double, Float number types - Quiz 14medium Functions - Nested functions - Quiz 12easy Loops - For-in with where clause - Quiz 12easy Swift Basics and Runtime - Main entry point and @main attribute - Quiz 4medium Swift Basics and Runtime - Why Swift for Apple and beyond - Quiz 10hard Variables and Constants - Var for variables (mutable) - Quiz 5medium