April 2026
Intermediate
683 pages
25h 29m
English
In Swift functions, the inout keyword allows a function to modify a parameter’s value. In the examples so far, we have either imported read-only parameters to a function or returned a brand-new value built in the function. inout features a middle ground, where you can read and change the value of a parameter.
Listing 5.45 demonstrates a simple function that accepts score as an inout parameter. Without the inout keyword, Swift would generate an error message indicating that score can’t be mutated within the function. Adding the inout parameter tells Swift that you intentionally made score mutable, allowing the function code to change its value.
func addPoints(_ points: Int, score: inout Int) { score += points}
Listing ...
Read now
Unlock full access