Functions
Functions are first-class citizens in Swift. This lesson covers parameters, return types, signatures, overloading, default parameters, variadic parameters, and inout behavior.
Key Terms
ParameterShow Definition
A named input to a function, treated as a local constant.
Return TypeShow Definition
The type of value a function outputs.
Function SignatureShow Definition
The full type of a function: parameters + return type.
ScopeShow Definition
Where a value can be accessed in code.
OverloadingShow Definition
Multiple functions with same name but different signatures.
inoutShow Definition
Allows a function to modify the caller’s variable directly.
Function Parameters & Return Values
1. What is the purpose of a function parameter?Show Solution
It provides input values to the function.
2. What does return do besides output a value?Show Solution
It immediately ends function execution.
3. Why must return types match exactly?Show Solution
Swift enforces strict type safety.
4. Why don’t external variable names affect function parameters?Show Solution
Parameters are locally bound inside the function call.
5. What happens if you ignore a return value?Show Solution
It is discarded; Swift may warn unless explicitly ignored.
Void Functions & No Parameters
6. What does Void mean?Show Solution
The function returns no value.
7. Why are parentheses still required for no-parameter functions?Show Solution
They indicate a function call.
8. Can a function have no parameters and no return?Show Solution
Yes.
9. When are Void functions useful?Show Solution
When performing actions like printing or logging.
Function Signatures
10. What is a function signature?Show Solution
The full type: parameters + return type.
11. Why are signatures important?Show Solution
They allow functions to be stored and passed as values.
12. What does (Int, Int) -> Int represent?Show Solution
A function taking two Ints and returning an Int.
External Parameter Names
13. What is an external parameter name?Show Solution
A label used when calling a function.
14. What does _ mean in a parameter list?Show Solution
It removes the external label requirement.
15. Why are labels useful?Show Solution
They improve readability.
Function Overloading
16. What is overloading?Show Solution
Multiple functions with the same name but different signatures.
17. How does Swift distinguish overloads?Show Solution
By parameter types and structure.
18. Can two functions differ only by return type?Show Solution
Not unless context disambiguates the call.
Default Parameters
19. What is a default parameter?Show Solution
A value used when no argument is provided.
20. What happens when omitted?Show Solution
Swift uses the default value.
Variadic Parameters
21. What is a variadic parameter?Show Solution
A parameter that accepts multiple values.
22. How is it used internally?Show Solution
As an array.
23. Can you pass an array directly?Show Solution
No, unless overloaded.
Inout Parameters
24. What does inout allow?Show Solution
Direct modification of caller variables.
25. Why is & required?Show Solution
To indicate mutation of external state.
26. What is shadowing a parameter?Show Solution
Creating a local copy of a parameter.
Concept Review
27. Why are parameters constants?Show Solution
To prevent unintended side effects.
28. Difference between local and inout modification?Show Solution
Local changes are temporary; inout affects caller state.
29. Why is inout powerful but risky?Show Solution
It introduces side effects that are harder to track.