What is the difference between an argument label and a parameter name in Swift?
An argument label is used when calling a function to clarify the meaning of each argument. A parameter name is used inside the function to refer to the passed value.
Click to reveal answer
beginner
How do you define a function in Swift with different argument labels and parameter names?
Use the syntax func functionName(argumentLabel parameterName: Type). For example: func greet(person name: String) { print("Hello, \(name)!") } where person is the argument label and name is the parameter name.
Click to reveal answer
intermediate
What happens if you use an underscore (_) as an argument label in Swift?
Using an underscore _ as the argument label means you don’t have to write the label when calling the function. It makes the argument label invisible to the caller.
Click to reveal answer
beginner
Why are argument labels useful in Swift functions?
Argument labels make function calls more readable and clear, like giving names to the values you pass. They help others understand what each argument means without looking inside the function.
Click to reveal answer
beginner
Show an example of a Swift function with no argument labels and explain how to call it.
Example: func add(_ a: Int, _ b: Int) -> Int { return a + b } You call it without labels: add(3, 5). The underscores mean no labels are needed when calling.
Click to reveal answer
In Swift, what is the purpose of an argument label?
ATo clarify the meaning of an argument when calling a function
BTo name the parameter inside the function
CTo declare the return type of a function
DTo define a variable inside the function
✗ Incorrect
Argument labels are used when calling a function to make the meaning of each argument clear.
How do you make a Swift function argument label invisible when calling the function?
AUse the keyword 'invisible'
BUse an underscore '_' as the argument label
COmit the parameter name
DUse the keyword 'hidden'
✗ Incorrect
Using '_' as the argument label hides it when calling the function.
Given the function: func greet(person name: String), how do you call it?
Agreet("Anna")
Bgreet(name: "Anna")
Cgreet(person: "Anna")
Dgreet(person name: "Anna")
✗ Incorrect
You use the argument label 'person' when calling: greet(person: "Anna")
What is the parameter name used for in Swift functions?
ATo label arguments when calling the function
BTo define the function name
CTo specify the return type
DTo refer to the argument value inside the function
✗ Incorrect
Parameter names are used inside the function to refer to the passed values.
Which of the following is a correct way to define a function with different argument label and parameter name?
Afunc sum(first a: Int, second b: Int) {}
Bfunc sum(a b: Int, c d: Int) {}
Cfunc sum(a: Int, b: Int) {}
Dfunc sum(_ a: Int, _ b: Int) {}
✗ Incorrect
Option A uses different argument labels ('first', 'second') and parameter names ('a', 'b').
Explain the difference between argument labels and parameter names in Swift and why they are useful.
Think about how you call a function versus how you use values inside it.
You got /4 concepts.
Write a Swift function that uses argument labels different from parameter names and show how to call it.
Use the syntax: func name(argumentLabel parameterName: Type)
You got /3 concepts.
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) { }.