About Swift Functions

From Coder Merlin
Jump to navigation Jump to search

Parameters, Arguments, and Labels[edit]

More Reading:


Consider the following function definition:

1func addTwoNumbers(a:Int, b:Int) -> Int {
2    return a + b
3}

Examine line 1, specifically the parameter list, a:Int, b:Int. "a" and "b" are termed the formal parameters to the function. Within the function body these formal parameters take on the value as specified by the caller when the function is invoked. By default, "a" and "b" become the external labels for the caller.

Now consider the following function invocation:

4addTwoNumbers(a:7, b:22)

Examine line 4, specifically the argument list, a:7, b:22. In this case "a" and "b" are the external labels, and the argument value of 7 is used for the parameter labeled by "a" and the value of 22 is used for the parameter labeled by "b".

It's important to understand that this relationship (label first, a semicolon, then the argument value) holds regardless of the actual value specified. Consider:

5let a = 7
6let b = 22
7addTwoNumbers(a:a, b:b)

Examine line 5 and line 6. Each defines an Integer constant. The constant "a" is assigned the value of 7, and the constant "b" is assigned the value of 22. Line 7 then invokes the function "addTwoNumbers" with two arguments. The argument list is a:a, b:b. The first "a" is the argument label, the second "a" is the argument value, a constant named "a" with the value of 7. Likewise, the first "b" is the argument label, the second "b" is the argument value, a constant named "b" with the value of 22.

Function Overloading[edit]

Consider the following function definitions:

 8func addTwoNumbers(a:Int, b:Int) -> Int {
 9    return a + b
10}
11
12func addThreeNumbers(a:Int, b:Int, c:Int) -> Int {
13    return a + b + c
14}
15
16func addFourNumbers(a:Int, b:Int, c:Int, d:Int) -> Int {
17    return a + b + c + d
18}
19
20print(addTwoNumbers(a:7, b:22))
21print(addThreeNumbers(a:7, b:22, c:43))
22print(addFourNumbers(a:7, b:22, c:43, d:87))

Note that the only difference between these functions is the number of parameters that each allows; the intent of each function is the same, i.e., to return the sum of the arguments. We have some syntactic sugar that we can use in these cases. Overloading enables us to use the same function name with a different parameter list, rather than force us to select different names. Continuing with our example, we may define the same functions as:

23func addNumbers(a:Int, b:Int) -> Int {
24    return a + b
25}
26
27func addNumbers(a:Int, b:Int, c:Int) -> Int {
28    return a + b + c
29}
30
31func addNumbers(a:Int, b:Int, c:Int, d:Int) -> Int {
32    return a + b + c + d
33}

These functions may then be invoked as:

35print(addNumbers(a:7, b:22))
36print(addNumbers(a:7, b:22, c:43))
37print(addNumbers(a:7, b:22, c:43, d:87))

Key Concepts[edit]

  • The parameter list is the list of constants (by default) in the function header. They are sometimes called formal parameters.
  • The value of each constant is determined by the caller when the function is invoked. The caller specifies an argument list; these arguments are sometimes called the actual parameters.
  • The argument list is specified by comma separated pairs of a label followed by the argument value.
  • Overloading provides us with the syntactic sugar to use the same function name with different argument lists when the intent of said functions is the same (or very similar).


Designed with pride in Silicon Valley, CA, USA