Swift Function Parameters
Korey Hinton -> Blog -> iOS -> Swift Function Parameters
Function Parameters
Named Parameters
Just like in objective-c, swift has named parameters but it looks different since the parameter naming happens within the parenthesis of the Swift method signature. Compare the difference below.
func append(string: String, withString: String) { return string + withString } println(append("Hello ","World")) //>> Hello World
-(NSString *)appendString:(NSString *)string withString:(NSString *)withString { return [string stringByAppendingString:withString]; } NSLog(@"%@",[self appendString:@"Hello " withString:@"World"]); //>> Hello World
External Parameter Names
When you use external parameter names it allows for the caller to see the name of the parameter. In the method signature the external and local parameter names will be specified and then just the local parameter name will be used in the body of the function.
class FramedObject { var frame : CGRect = CGRectZero func resize(frame fr: CGRect) { self.frame = fr } } var myFramedObject = FramedObject() myFramedObject.resize(frame: CGRectMake(0,0,200,200))
Shorthand External Parameter Names
Use shorthand external parameter names when you want the local and external parameter names to be the same. Instead of writing it twice you can use the shorthand version.
class PositionedObject { var point : CGPoint = CGPointZero func moveTo(#point: CGPoint) { self.point = point } } var positionedObject = PositionedObject() positionedObject.moveTo(CGPointMake(5,5))
Variadic Parameters
Swift makes it easy to pass multiple objects of the same type into the method using … notation.
func sum(integers: Int...) -> Int { var sum : Int = 0 for integer in integers { sum += integer } return sum } func append(strings: String...) -> String { var joinedString : String = "" for str in strings { joinedString += str } return joinedString } var a = 5 var b = 4 var c = 1 var output = append("a + b + c = ","\(sum(a,b,c)") println(output) //>> a + b + c = 10
Optional Parameters aka Default Parameter Values
If you want a parameter to be optional to the caller you can assign it a default value right in the method signature using the equal sign.
func move(position pos: CGPoint = CGPointZero) { self.position = pos } self.move() // Moves to (0,0) self.move(CGPointMake(3,5)) // Moves to (3,5)
Non-constant Variable Parameters
By default you can't modify a parameter's value in the function body because it is a constant. You can explicitly define it as a var and thereby have a copy of the parameters value instead.
func compute(var sum1: Int, sum2: Int) -> Int { sum1 += sum2 return sum1 } println(\(compute(1,3))) //>> 4
In-out Parameters
Also you can pass in a variable by reference using in-out parameters and get the changed value back. The caller uses an & to indicate it is being passed by reference and the method signature uses the inout keyword to signify it is being passed by reference.
func intDivide(num1: Int, num2: Int, inout remainder:Int) -> Int { remainder = num1 % num2 return num1/num2 } var remainder = 0 var a : Int = 5 var b : Int = 2 var bInA = intDivide(a,b,&remainder) println("\(remainder)") //>> 1
As you can see Swift has many different types of function parameters and this gives you a lot of flexibility for functional programming.