Function Parameters

Functions take on more life when they have parameters. A function’s parameters name the inputs that the function accepts, and 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.

Example 12.2. Using a parameter
func printGreeting() {
     print("Hello, playground.")
}
printGreeting()


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

printPersonalGreeting(name: String) has a single parameter, as indicated in the parentheses directly after the function name. The parameter is called name, and it is an instance of the String type. ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.