O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Function Parameters

Functions begin to take on more life when they have parameters. You use parameters to give a function some inputs. We call these parts of a function parameters to indicate that they can change value depending on the data the caller passes into the function. The function takes the data passed to its parameters to execute a task or produce a result.

Create a function that prints a more personal greeting by using a parameter.

Listing 12.2 Using a parameter

import Cocoa

func printGreeting() {
     print("Hello, playground.")
}
printGreeting()

func printPersonalGreeting(name: String) {
     print("Hello \(name), welcome to your playground.")
}
printPersonalGreeting(name: "Matt")

printPersonalGreeting(name:) takes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required