📖 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
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
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
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
Hint
Call print(greet(to: "Bob", yearsOld: 25)) to print directly.
Practice
(1/5)
1. What is the purpose of an argument label in a Swift function?
easy
A. To store the return value of the function
B. To define the variable type inside the function
C. To clarify the meaning of each argument when calling the function
D. To create a new function inside another function
Solution
Step 1: Understand argument labels
Argument labels are used outside the function when calling it to make the call clearer.
Step 2: Differentiate from parameter names
Parameter names are used inside the function, but argument labels help the caller understand what each value means.
Final Answer:
To clarify the meaning of each argument when calling the function -> Option C
Quick Check:
Argument label = clarify call [OK]
Hint: Argument labels clarify calls, parameter names work inside [OK]
Common Mistakes:
Confusing argument labels with parameter names
Thinking argument labels define variable types
Assuming argument labels store return values
2. Which of the following is the correct way to declare a function with an argument label from and a parameter name start of type Int?
easy
A. func move(from: start Int) { }
B. func move(from start: Int) { }
C. func move(start from: Int) { }
D. func move(Int from start) { }
Solution
Step 1: Recall Swift function syntax for labels
The syntax is: func name(argumentLabel parameterName: Type) { }.