Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Functions and Optionals Parameters and Tuples Named Parameters

jon kelson
jon kelson
5,149 Points

Hi could someone help func greeting(person: "person") { println("Hello \(person)") } greeting(person:"Jerry")

hi im stuck on this one call the greeting function thanks Jon

named_parameters.swift
func greeting(person: "person") {
    println("Hello \(person)")
}
greeting(person:"Jerry")​func greeting(person: "person") {
    println("Hello \(person)")
}
greeting(String: "Jerry")

1 Answer

Matthew Young
Matthew Young
5,133 Points

First, the function you declared called "greeting" shouldn't have "person" in there even with the quotes. After the colon, you want to declare the type of the variable that's being passed through. If you're trying to create a named parameter, you need to add the term "person" twice before the colon. For example:

func greeting(person person: String) {
    println("Hello \(person)")
}

The first "person" represents that external parameter name and the second "person" represents the internal parameter name. The external parameter name is what appears when you are calling the function. The internal parameter name is what's used as a local variable within the function itself.

To explain, this function takes in one parameter that is of type String called "person". This function prints out "Hello 'person'" with the term 'person' replaced by whatever String you passed through in the parameter. For example, when you call that function with the following code:

greeting(person: "Jerry")

The function prints out "Hello Jerry"

I hope this helps, you can find more info on this topic at: Swift Programming Language: Functions. Specifically, look under the subsection called "Specifying External Parameter Names".