Bird
0
0

You want to declare a Swift function named describePerson that takes a String parameter name and an Int parameter age, and returns a String describing the person like "Name is Age years old." Which is the correct function declaration?

hard📝 Application Q15 of 15
Swift - Functions
You want to declare a Swift function named describePerson that takes a String parameter name and an Int parameter age, and returns a String describing the person like "Name is Age years old." Which is the correct function declaration?
Afunc describePerson(name: String, age: Int) -> String { return "\(name) is \(age) years old." }
Bfunc describePerson(name String, age Int) -> String { return "\(name) is \(age) years old." }
Cfunc describePerson(name: String, age: Int) { print("\(name) is \(age) years old.") }
Dfunc describePerson(name: String, age: Int) -> Void { return "\(name) is \(age) years old." }
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter syntax and return type

    Parameters must have colons between name and type. The function returns a String, so return type must be String.
  2. Step 2: Verify function body matches return type

    func describePerson(name: String, age: Int) -> String { return "\(name) is \(age) years old." } returns a String with correct string interpolation. func describePerson(name String, age Int) -> String { return "\(name) is \(age) years old." } misses colons. func describePerson(name: String, age: Int) { print("\(name) is \(age) years old.") } has no return type and uses print instead of return. func describePerson(name: String, age: Int) -> Void { return "\(name) is \(age) years old." } uses Void return type but returns a String, which is invalid.
  3. Final Answer:

    func describePerson(name: String, age: Int) -> String { return "\(name) is \(age) years old." } -> Option A
  4. Quick Check:

    Correct parameters and return type with string interpolation [OK]
Quick Trick: Use colons for parameters and match return type with returned value [OK]
Common Mistakes:
  • Omitting colons in parameters
  • Using print instead of return for returning values
  • Mismatching return type and returned value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes