Swift - FunctionsWhich of the following Swift function declarations correctly uses a default parameter value?Afunc greet(name: String = "Guest") { print("Hello, \(name)!") }Bfunc greet(name: String) = "Guest" { print("Hello, \(name)!") }Cfunc greet(name: String default "Guest") { print("Hello, \(name)!") }Dfunc greet(name: String) { print("Hello, \(name = Guest)!") }Check Answer
Step-by-Step SolutionSolution:Step 1: Review Swift default parameter syntaxThe correct syntax is to assign a default value with '=' inside the parameter list.Step 2: Check each optionOnly func greet(name: String = "Guest") { print("Hello, \(name)!") } uses the correct syntax: name: String = "Guest".Final Answer:func greet(name: String = "Guest") { print("Hello, \(name)!") } -> Option AQuick Check:Default parameter syntax = parameter = value [OK]Quick Trick: Use '=' to assign default values in parameter list [OK]Common Mistakes:Using '=' outside parameter listWriting 'default' keywordAssigning default inside function body
Master "Functions" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Collections - Set algebra (union, intersection, difference) - Quiz 1easy Collections - Dictionary methods and default values - Quiz 15hard Control Flow - Switch with compound cases - Quiz 9hard Control Flow - Guard for early exit pattern - Quiz 10hard Functions - Why functions are first-class in Swift - Quiz 12easy Functions - Why functions are first-class in Swift - Quiz 4medium Loops - For-in loop with collections - Quiz 15hard Operators and Expressions - Operator overloading concept - Quiz 15hard Operators and Expressions - Comparison operators - Quiz 1easy Optionals - Nil coalescing operator (??) - Quiz 4medium