Declare a function with inout parameters

 Problem

I want to declare a function with parameters which it can alter, and call it with a reference.



 Recipe


// Declare a function with 'inout' parameters
func append(inout str:String, withString:String) {
    str += withString
}
    
// Declare string variable
var myString = "Hello"

// Call the function with a reference - &
// to our 'myString' variable
append(&myString, withString:" World!")

// The string variable will have now changed
print(myString)
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground