Parameters And Arguments
Part of the Fundamentals section of Coddy's Swift journey — lesson 52 of 86.
Functions become much more powerful when they can accept input. Parameters are placeholders defined in the function declaration, while arguments are the actual values you pass when calling the function.
To add a parameter, specify its name and type inside the parentheses:
func greet(name: String) {
print("Hello, \(name)!")
}
greet(name: "Alice") // Output: Hello, Alice!
greet(name: "Bob") // Output: Hello, Bob!Here, name: String is the parameter—it tells Swift the function expects a String value. When calling the function, you provide the argument using the parameter name followed by a colon.
Functions can accept multiple parameters, separated by commas:
func introduce(name: String, age: Int) {
print("\(name) is \(age) years old.")
}
introduce(name: "Sara", age: 25)
// Output: Sara is 25 years old.Each parameter acts like a constant inside the function body—you can use it but not modify it. The order of arguments must match the order of parameters in the function definition.
Challenge
EasyWrite a function createFullName that takes firstName and lastName and returns the full name as a single string.
Combine the two names with a space between them.
Parameters:
firstName(String): The person's first namelastName(String): The person's last name
Returns: The full name with a space between first and last name (String)
For example, if firstName is "John" and lastName is "Doe", the function returns "John Doe".
Cheat sheet
Functions can accept input through parameters. Parameters are placeholders defined in the function declaration with a name and type:
func greet(name: String) {
print("Hello, \(name)!")
}
greet(name: "Alice") // Output: Hello, Alice!When calling a function, you pass arguments using the parameter name followed by a colon.
Functions can accept multiple parameters, separated by commas:
func introduce(name: String, age: Int) {
print("\(name) is \(age) years old.")
}
introduce(name: "Sara", age: 25)
// Output: Sara is 25 years old.Parameters act like constants inside the function body and cannot be modified. The order of arguments must match the order of parameters.
Try it yourself
func createFullName(firstName: String, lastName: String) -> String {
// Write code here
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input