Start learning free

LESSON 36 OF 349 · FUNCTIONS I

Function parameters & arguments in Swift in Swift

Lesson

So far, the functions we've created execute a fixed task without context / inputs. But most often we want our functions to produce different outcomes depending on the context behind the function call.

For example, it would be nice if sayHello() would print a personalized message such as "Hello humans" or "Hello cats" or "Hello dogs", depending on the audience instead of "Hello world" no matter what.


Function parameters:

Function parameters allow functions to accept input(s) and perform a task using the input(s). We use parameters as placeholders for information that will be passed to the function when it is called.

When a function is defined, its parameters are specified between the parentheses that follow the function name.

Here is our function with one String parameter, audience:

func sayHello(audience: String) {
    print("Hello \(audience)")
}

Calling with arguments:

Then we can call sayHello() and specify the values in the parentheses that follow the function name. The values that are passed to the function when it is called are called arguments.

// Function declaration:
func sayHello(audience: String) {
    print("Hello \(audience)")
}

// Function call:
sayHello(audience: "humans") // Output: Hello humans

We have passed one string argument: "humans". Inside the function audience will equal string "humans" and acts just like a regular variable.


Argument labels & the _ underscore:

Notice that when we call the function we repeat the parameter's name: sayHello(audience: "humans"). That name at the call site is the argument label — by default Swift uses each parameter's name as its label.

If you'd rather call the function without a label, write an underscore _ before the parameter name. The _ means "no argument label":

func sayHello(_ audience: String) {
    print("Hello \(audience)")
}

sayHello("humans") // Output: Hello humans  — no label needed

You'll see this _ throughout the coding tutorials, e.g. func magicNumber(_ n: Int) -> Int, which lets you call magicNumber(1998) instead of magicNumber(n: 1998). Inside the function you still refer to the parameter by its name (audience, n).


Multiple parameters:

A function can have as many parameters as it needs. Here is a function with two parameters:

func sayHello(name: String, age: Int) {
    print("\(name), aged \(age)")
}

// Let's call it:
sayHello(name: "John", age: 30) // Output: John, aged 30
sayHello(name: "Mary", age: 26) // Output: Mary, aged 26

Notice that the order in which arguments are passed and assigned follows the order that the parameters are declared.


Variables as arguments:

Variables can also be passed as arguments for function calls:

func sayHello(name: String, age: Int) {
    print("\(name), aged \(age)")
}

let name = "Andy"
let age = 28

sayHello(name: name, age: age) // Output: Andy, aged 28

sayHello(name: "Mary", age: age) // Output: Mary, aged 28

Parameters are constants:

Inside a function, its parameters are constants (like a let) — you cannot reassign them. This does not compile:

func countdown(_ n: Int) {
    n = n - 1 // error: cannot assign to 'n' — it is a constant
}

If you need to change the value, make a mutable copy with var. Re-using the same name simply shadows the parameter:

func countdown(_ n: Int) {
    var n = n // a mutable copy you can change
    while n > 0 {
        print(n)
        n -= 1
    }
}

That's why many of the coding tutorials begin a function with a line like var n = n.


Assignment
Follow the Coding Tutorial and let's write some functions.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    Function Parameters And Arguments in Swift

    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free