0
0
Swiftprogramming~15 mins

Argument labels and parameter names in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Argument labels and parameter names
📖 Scenario: You are creating a simple Swift program to greet people with customized messages. You want to practice how to use argument labels and parameter names in functions to make your code clear and easy to read.
🎯 Goal: Build a Swift function that uses argument labels and parameter names correctly to greet a person with their name and age, then call the function to display the greeting.
📋 What You'll Learn
Create a function with argument labels and parameter names
Call the function using argument labels
Print the greeting message
💡 Why This Matters
🌍 Real World
Using argument labels and parameter names helps make your Swift code easier to read and understand, especially when working on apps with many functions.
💼 Career
Clear function signatures with argument labels are important in professional Swift development to improve code maintainability and collaboration.
Progress0 / 4 steps
1
Create the greeting function
Write a function called greet that takes two parameters: name of type String and age of type Int. Use argument labels to for name and yearsOld for age. The function should return a String greeting message formatted as: "Hello, \(name)! You are \(age) years old."
Swift
Need a hint?

Use the syntax func functionName(argumentLabel parameterName: Type) to define argument labels and parameter names.

2
Call the greet function
Call the function greet with argument label to set to "Alice" and argument label yearsOld set to 30. Store the result in a variable called message.
Swift
Need a hint?

Use the argument labels exactly as defined when calling the function.

3
Print the greeting message
Print the variable message to display the greeting on the screen.
Swift
Need a hint?

Use print(message) to show the greeting.

4
Try calling greet with different values
Call the function greet again with argument label to set to "Bob" and argument label yearsOld set to 25. Print the result directly without storing it in a variable.
Swift
Need a hint?

Call print(greet(to: "Bob", yearsOld: 25)) to print directly.