0
0
Swiftprogramming~10 mins

Default parameter values in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default parameter values
Function call
Check if argument provided?
NoUse default value
Yes
Use provided argument
Execute function body with argument
Return result or complete
When calling a function, Swift checks if an argument is given. If not, it uses the default value. Then it runs the function with that value.
Execution Sample
Swift
func greet(name: String = "Friend") {
    print("Hello, \(name)!")
}

greet()
greet(name: "Anna")
This code defines a function with a default name. It prints a greeting using the default or given name.
Execution Table
StepFunction CallArgument Provided?Parameter Value UsedOutput
1greet()No"Friend"Hello, Friend!
2greet(name: "Anna")Yes"Anna"Hello, Anna!
3End--No more calls
💡 No more function calls, execution ends.
Variable Tracker
VariableStartAfter Call 1After Call 2Final
name-"Friend""Anna"-
Key Moments - 2 Insights
Why does greet() print "Hello, Friend!" even though no argument is given?
Because the function has a default value "Friend" for the parameter 'name', it uses this when no argument is provided (see execution_table row 1).
What happens if we provide an argument like greet(name: "Anna")?
The function uses the provided argument "Anna" instead of the default (see execution_table row 2), so it prints "Hello, Anna!".
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the parameter value used when greet() is called without arguments?
A"Friend"
B"Anna"
Cnil
DEmpty string
💡 Hint
Check execution_table row 1 under 'Parameter Value Used'
At which step does the function use the provided argument instead of the default?
AStep 1
BStep 3
CStep 2
DNone
💡 Hint
Look at execution_table row 2 'Argument Provided?' column
If we remove the default value from the function, what will happen when calling greet() without arguments?
AIt will print "Hello, Friend!"
BIt will cause a compile error
CIt will print "Hello, !"
DIt will use nil as the name
💡 Hint
Default values allow calls without arguments; removing it requires argument (see concept_snapshot)
Concept Snapshot
func functionName(parameter: Type = defaultValue) {
  // function body
}

- If argument is missing, defaultValue is used.
- If argument is given, it overrides default.
- Helps avoid writing multiple function versions.
Full Transcript
This visual shows how Swift functions use default parameter values. When you call a function without giving an argument, Swift uses the default value you set. If you provide an argument, it uses that instead. The example greet() prints a greeting using the default name "Friend" if no name is given, or the provided name otherwise. The execution table traces each call, showing the parameter value used and the output. The variable tracker shows how the 'name' parameter changes with each call. Key moments clarify why default values matter and how they work. The quiz tests understanding by asking about parameter values and behavior when defaults are removed.