In our previous functions, we’ve sometimes used the values of variables as arguments. While we could return an entirely new value, we couldn’t directly affect the variable used as the argument. An in-out parameter allows a function to reassign the value of a variable passed in as an argument.
Since a function with an inout
parameter is expected to change the value of its argument, only variables can be passed in when the function is called - not constants or literals since they can not be altered.
An inout
parameter is declared in the function definition after the parameter name and before its specified type:
func funcName(parameterName: inout parameterType) -> returnType {
Say we have a program that changes the color of a phone battery depending on the charge percentage of the phone. We can set up the following function that utilizes an inout
parameter:
var currentColor = "green" func batteryInterface(percentage: Int, batteryColor: inout String) { if percentage < 5 { batteryColor = "red" } else if percentage < 15 { batteryColor = "yellow" } else { batteryColor = "green" } }
Currently, the phone’s battery color is "green"
. “The function holds two parameters, percentage
and an inout
parameter, batteryColor
. Within the body of the function, we use a conditional to assign a color to batteryColor
. Notice how we don’t return any values from the function.
To spring this function into action, we must call it in the following form:
batteryInterface(percentage: 12, batteryColor: ¤tColor)
When a function that utilizes an inout
parameter is called, an ampersand, &
, must be used directly before the variable name that will act as its argument. This symbol instructs the program that the variable can be modified.
The real value for percentage
is set to 12
, and the if
statement within the function executes. batteryColor
becomes "yellow"
and the variable, currentColor
takes on that new value.
print(currentColor) // Prints: yellow
Instructions
Many buildings throughout New York have power generators used for backup during power outages. In the code editor, set up a function that will determine whether a building’s power generator should be switched on.
- Define the function,
generators()
, that will accept apowerOutage
parameter of the type,Bool
. generators()
will also accept aninout
parameter namedstate
of the type,String
.- The function should not return any values.
Within the function body, create an if
/else
statement that will asses the Boolean value of powerOutage
.
- If
powerOutage
istrue
,state
should store,"On"
. - Otherwise,
state
should store,"Off"
.
Assume we’ve traveled back in time to August 14, 2003 - the first day of the Northeast blackout of 2003, and we need to switch on those generators immediately.
Following the function body, invoke the generators()
function.
- For the
powerOutage
parameter, pass in the argument,true
to reflect the outage. - For the
state
parameter, the argument should be¤tGeneratorState
. The function will execute and modify the value of thecurrentGeneratorState
as needed.
Lastly, print the value of currentGeneratorState
to see the change.