0
0
Swiftprogramming~5 mins

In-out parameters for mutation in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an in-out parameter in Swift?
An in-out parameter allows a function to modify a variable passed to it, so the changes persist outside the function.
Click to reveal answer
beginner
How do you declare an in-out parameter in a Swift function?
Use the inout keyword before the parameter type in the function definition.
Click to reveal answer
beginner
How do you pass a variable as an in-out argument when calling a Swift function?
Use the ampersand & before the variable name to pass it as an in-out argument.
Click to reveal answer
intermediate
Why can't you pass a constant or a literal as an in-out argument?
Because in-out parameters require the ability to modify the variable, constants and literals cannot be changed.
Click to reveal answer
beginner
What happens if you forget to use & when passing an in-out argument?
The Swift compiler will give an error because it expects an explicit indication that the variable can be modified.
Click to reveal answer
Which keyword is used to declare an in-out parameter in Swift?
Ainout
Bmutate
Cvar
Dref
How do you pass a variable as an in-out argument when calling a function?
AWith parentheses around the variable
BWith an asterisk (*) before the variable
CJust the variable name
DWith an ampersand (&) before the variable
Can you pass a constant as an in-out argument?
AYes, always
BOnly if the function returns a value
CNo, because it cannot be modified
DYes, but only if marked with <code>let</code>
What is the main purpose of in-out parameters?
ATo allow a function to modify the caller's variable
BTo return multiple values from a function
CTo make parameters optional
DTo improve performance by copying variables
What error occurs if you forget the & when passing an in-out argument?
ASyntax error
BCompiler error
CRuntime crash
DNo error, it works fine
Explain how in-out parameters work in Swift and why they are useful.
Think about how a function can change a variable outside its own scope.
You got /4 concepts.
    Describe the syntax for declaring and using an in-out parameter in a Swift function.
    Focus on the keywords and symbols needed.
    You got /3 concepts.