Functions | Coding With Matt`

Functions

Contents

Functions are first class citizens

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

Parameter
Show DefinitionA named input to a function, treated as a local constant.
Return Type
Show DefinitionThe type of value a function outputs.
Function Signature
Show DefinitionThe full type of a function: parameters + return type.
Scope
Show DefinitionWhere a value can be accessed in code.
Overloading
Show DefinitionMultiple functions with same name but different signatures.
inout
Show DefinitionAllows a function to modify the caller’s variable directly.

Function Parameters & Return Values

1. What is the purpose of a function parameter?
Show SolutionIt provides input values to the function.
2. What does return do besides output a value?
Show SolutionIt immediately ends function execution.
3. Why must return types match exactly?
Show SolutionSwift enforces strict type safety.
4. Why don’t external variable names affect function parameters?
Show SolutionParameters are locally bound inside the function call.
5. What happens if you ignore a return value?
Show SolutionIt is discarded; Swift may warn unless explicitly ignored.

Void Functions & No Parameters

6. What does Void mean?
Show SolutionThe function returns no value.
7. Why are parentheses still required for no-parameter functions?
Show SolutionThey indicate a function call.
8. Can a function have no parameters and no return?
Show SolutionYes.
9. When are Void functions useful?
Show SolutionWhen performing actions like printing or logging.

Function Signatures

10. What is a function signature?
Show SolutionThe full type: parameters + return type.
11. Why are signatures important?
Show SolutionThey allow functions to be stored and passed as values.
12. What does (Int, Int) -> Int represent?
Show SolutionA function taking two Ints and returning an Int.

External Parameter Names

13. What is an external parameter name?
Show SolutionA label used when calling a function.
14. What does _ mean in a parameter list?
Show SolutionIt removes the external label requirement.
15. Why are labels useful?
Show SolutionThey improve readability.

Function Overloading

16. What is overloading?
Show SolutionMultiple functions with the same name but different signatures.
17. How does Swift distinguish overloads?
Show SolutionBy parameter types and structure.
18. Can two functions differ only by return type?
Show SolutionNot unless context disambiguates the call.

Default Parameters

19. What is a default parameter?
Show SolutionA value used when no argument is provided.
20. What happens when omitted?
Show SolutionSwift uses the default value.

Variadic Parameters

21. What is a variadic parameter?
Show SolutionA parameter that accepts multiple values.
22. How is it used internally?
Show SolutionAs an array.
23. Can you pass an array directly?
Show SolutionNo, unless overloaded.

Inout Parameters

24. What does inout allow?
Show SolutionDirect modification of caller variables.
25. Why is & required?
Show SolutionTo indicate mutation of external state.
26. What is shadowing a parameter?
Show SolutionCreating a local copy of a parameter.

Concept Review

27. Why are parameters constants?
Show SolutionTo prevent unintended side effects.
28. Difference between local and inout modification?
Show SolutionLocal changes are temporary; inout affects caller state.
29. Why is inout powerful but risky?
Show SolutionIt introduces side effects that are harder to track.