Open menu with table of contents Swift Function Parameters and Labels
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Mobile Application Development 2

Swift Function Parameters and Labels

NOTE: This is the playground we did within the lesson. Just a bit more sugar coating.

Code

OVERARCHING EXAMPLE: We have an Animal that we want to transport from one location to another location

Stuttgart Media University
import Foundation

// We want a function that is descriptive and easy to read
// OVERARCHING EXAMPLE: We have an Animal that we want to transport from one location to another location

// Option 1: Very descriptive function name, but becomes repetitive when called
// This is most likeley how you would have written the function in Java.
func moveAnimalTo(animal: String, from: String, to: String) {}
 
func transportAnimalFromTo(animal: String, from: String, to: String) {}

// You can read this as
// Transport Animal From To Animal "Doggo" from "Dublin" to "Stuttgart

// Option 2: Shorter function name with descriptive parameter labels
func moveAnimal(animal: String, from: String, to: String){}

// You can already read this a bit better
// Move Animal Animal: "Doggo" from: "Stuttgart" to: "Dublin.

//As in Java you can also ommit parameter names like:
func moveAnimalFromTo(_ animal: String, _ from: String, _ to: String){}

// This '_' will tell the programm that it can be set without a parameter name. It would read like
// Move Animal From To "Doggo" "Stuttgart" "Dublin"

// Option 3: Using more descriptive parameter names
func moveAnimal(animal: String, start: String, destination: String){}

// Option 4: Using underscore (_) to omit the first parameter label
// This makes the call read more naturally: moveAnimal("Doggo", ...)
func moveAnimal(_ animal: String, start: String, destination: String){}
moveAnimal("Doggo", start: "Berlin", destination: "Stuttgart")

// You could read it as
// Move Animal "Doggo" start "Berlin" destination "Stuttgart"

// Function Overloading: Same function name, different parameter labels
// Swift knows which function to call based on the parameter labels used
func move(car: String, from: String, to: String){}
func move(animal: String, from: String, to: String){}

// These call different functions despite having the same base name
move(animal: "Doggo", from: "New York", to: "Stuttgart")
move(car: "Renault", from: "Berlin", to: "Rotterdam")

// This would read out loud something like
// Move Animal "Doggo" from "New York" to "Stuttgart" - which is quite good as an english sentence

moveAnimal(animal: "Doggo", start: "Stuttgart", destination: "Berlin")

1 External / Internal Parameters

// External vs Internal Parameter Names
// Syntax: func name(externalName internalName: Type)
// - externalName: used when calling the function
// - internalName: used inside the function body

func move(your animal: String, from location: String, to destination: String) {
    print(animal)      // Use internal name inside function
    print(location)    // Use internal name inside function
    print(destination) // Use internal name inside function
}
// "your" is external and thats how the function needs to be called
// "animal" is internal and how you would reference it internally

// Default Parameter Values
// Parameters can have default values, making them optional when calling the function
// If you don't provide a value, the default is used

//func move(your animal: String  = "Lizzard", from location: String = "Germany", to destination: String = "Sweden") {
//    print(animal)
//    print(location)
//    print(destination)
//}

// Simplified version: When external and internal names are the same
func move(your: String  = "Lizzard", from: String = "Germany", to: String = "Sweden") {
    print(your)
    print(from)
    print(to)
}
// This shows the issue not having internal and external parameters. It is way easier to read but inside the function it looks quite ugly.

// Calling with all parameters
move(your: "doggo", from: "Wohnzimmer", to: "Küche")

// Calling with no parameters - uses all defaults
move()

2 Expected Output

Lizzard
Germany
Sweden

3 Key Takeaways

  1. Underscore (_): Omits the parameter label when calling

    • func greet(_ name: String) → called as greet("John")
  2. Parameter Labels: Make function calls read like sentences

    • moveAnimal("Doggo", start: "Berlin", destination: "Stuttgart")
  3. External vs Internal Names: func move(your animal: String)

    • your = external (used when calling)
    • animal = internal (used inside function)
  4. Default Values: Make parameters optional

    • func move(your: String = "Lizzard") → can call move() or move(your: "cat")
  5. Function Overloading: Same name, different parameter labels

    • Swift chooses the right function based on the labels you use