This is second quick tutorial on Swift Functions on iOS Scholar’s Blog. The previous tutorial on Functions in Swift included Multiple Return Types and External Parameters. In this tutorial we’ll cover the Shorthand External Parameters, Variable Parameters and Inout Parameters.

Shorthand External Parameters

In the previous post, we used external parameters to improve readability and make function usage more intuitive. However, the additional names for parameters to be used as labels can be regarded as overhead at times. This is specifically when the internal parameter name itself is apt to be used as external parameter too. In such cases, we can use the Shorthand - we simply prefix the parameter name with a hash symbol to indicate that it’s both an external and internal parameter name.

Let’s consider the example below. These two functions calculatePower and interestCalculator both have one or more parameters prefixed with hash symbols. While calling these functions, we have to use the labels for parameters which specify hash symbols.

In the interestCalculator function above, we could choose a default rateOfInterest by specifying a value in the function declaration itself, and then we could omit this parameter while invoking the function:

Notice that we’ve not used the shorthand for external parameter - hash for rateOfInterest parameter. That’s because default parameters are implicitly external and will have to be used as labels while invoking a function - unless you choose to use the default value, in which case, the parameter is omitted altogether.

Let’s consider the following function calls. The first uses labels and specifies all parameters, while the second call doesn’t specify the second parameter, and thus uses the default value of rateOfInterest.

An important thing to note here is the order of parameters. It’s advisable to put all the default parameters towards the right. (which is intentionally not done in the example above) In other words, default parameters should be kept trailing in the list of parameters, so that, when you omit the default parameters while invoking the function, there would be no ambiguity for mapping the arguments to parameters.Keeping the parameters with default values in the right is not just a convention, but compulsory in C/C++.

However, in Swift, even if your default parameters are not in the right, the function call will still be unambiguous, like in the example above, if just one parameter immediately following the right of default parameter have external parameters (or shorthand external parameters). In the function call above, since numberOfYears parameter is labelled, it’s clear that the rateOfInterest parameter is omitted, and since it has a default value, that will be used, making the function call unambiguous.

If for some reason, you want to escape default behavior of implicit external parameter for parameters with default values, use an underscore before the parameter name:

The error is caused parameter number which is a “let value” by default, which means, number is a constant whose value can not be modified. But if we change the parameter’s value type to ‘var’, the function would then compile successfully:

func doubleNumber(var number: Int) -> Int

{

number = number * 2

return (number)

}

The parameter number is now a variable parameter. (The outputs remain the same, we are now returning number, instead of number * 2)

Variable Parameters and Inout Parameters in Swift Functions

Inout Parameters

In the above example, we changed the parameter’s value type to variable, and thus it’s mutable within the function. However, the parameter is still a copy of the variable which is used to invoke the function. Thus, the original value remains unchanged.

In the code fragment above, we called the function doubleNumber with x as a parameter, but since the doubled value doesn’t get reflected back, we assign the doubled value to x. However, if we could reflect the modified value of x, then the returning and assigning value are both unnecessary.

To reflect the values back in the calling function, we use “inout parameters” in Swift.

func doubleNumber(inout number: Int)

{

number *= 2

}

var x = 10

doubleNumber(&x)

println(x)

//Prints: 20

//println(doubleNumber(3))

//error: could not find an overload for 'println' that accepts the supplied arguments

Now, no value is returned from the function, we simply invoke doubleNumber() with parameter x, and the variable x gets modified accordingly. The ampersand before the parameter is compulsory - it signifies the “acceptance” of the modification that called function can make to the parameter. Looking at it the other way round, somebody reading your code, will take a look at the ampersand and know that this function takes an inout parameter.

Notice the commented error above. Since inout parameters can be modified by the called function, only variables can be sent when inout parameters are expected in the function call. Literals like “3” or “let” values can not be sent to doubleNumber()

Conclusion

In this tutorial, we discussed Shorthand External Parameters, which eliminate the requirements of separate external and internal names for parameters. Next, we checked out default parameters and some interesting stuff about how order of default parameters can make the function call ambiguous. Then, we changed the default immutable behaviour of function parameters in Swift, using default parameters. Finally, we discussed inout parameters, which let the value of parameter reflect back in the calling function / scope. That's all for this tutorial. Have fun with Swift. And please use the comments box for any feedback you would like to give.

Annotate Parameters, Returned Values and more...
Functions in Swift, Apple's new programming language, are extremely flexible and very powerful. They can return multiple values and external parameters can be used to label the parameters while invoking the functions.