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

Stuck on this test. Do not really know were to begin. Looks like they already have a string parameter named person.

Maybe my though process is incorrect. But looks like they already have a String parameter named person. Do not know were to begin. If someone could lead me in the right direction would really appreciate it

named_parameters.swift
var person = "Tom"
func greeting(person) {
    println("Hello \(person)")
}

2 Answers

Stone Preston
Stone Preston
42,016 Points

from the swift ebook:

If you want users of your function to provide parameter names when they call your function, define an external parameter name for each parameter, in addition to the local parameter name. You write an external parameter name before the local parameter name it supports, separated by a space:

func someFunction(externalParameterName localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

If you want to provide an external parameter name for a function parameter, and the local parameter name is already an appropriate name to use, you do not need to write the same name twice for that parameter. Instead, write the name once, and prefix the name with a hash symbol (#). This tells Swift to use that name as both the local parameter name and the external parameter name.

right now it just has an internal parameter. you can add an external parameter with the same name as the internal by adding a # in front of the parameter name

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

by adding an external parameter, you mandate that the parameter name must be provided in the function call.

before adding the external parameter the function call would have looked like this:

greeting("Stone")

After adding the external parameter name you have to provide the parameter name in the function call

greeting(person: "Stone")

see the swift ebook for reference

Thanks for the help Stone Preston, good job accumulating so many pt in treehouse. If you don't mind me asking, how long did it take you. Anyways thanks gain for the help, you were very fast and extremely helpful.

Kind Regards Phong Tran

Stone Preston
Stone Preston
42,016 Points

I joined about a year and a couple months ago.