Bird
0
0

Which of the following is the correct syntax to declare a function named greet that takes no parameters and returns nothing?

easy📝 Syntax Q12 of 15
Swift - Functions
Which of the following is the correct syntax to declare a function named greet that takes no parameters and returns nothing?
Afunc greet() -> void {}
Bfunction greet() {}
Cfunc greet {}
Dfunc greet() {}
Step-by-Step Solution
Solution:
  1. Step 1: Check function declaration syntax

    In Swift, a function with no parameters and no return type is declared as func name() {}.
  2. Step 2: Analyze each option

    func greet() {} matches the correct syntax. function greet() {} uses 'function' which is invalid. func greet {} misses parentheses. func greet() -> void {} uses 'void' which is not used in Swift.
  3. Final Answer:

    func greet() {} -> Option D
  4. Quick Check:

    Empty parameter list needs () and no return type means no arrow [OK]
Quick Trick: Empty parameters always need () after function name [OK]
Common Mistakes:
  • Omitting parentheses for parameters
  • Using 'function' instead of 'func'
  • Using 'void' as return type in Swift

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes