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

Confused a little

I answered the objective correctly however Im curious about what makes the parameter external? or am I looking to deep into it.

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

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Your parameter is always external, but with swift you can choose to give it an external parameter name.

In case of Swift 1.2, the # is shorthand for

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

that is, shorthand for naming the external parameter just like the internal parameter.

So what are internal and external paramter names?

  • Internal parameter name The name of the parameter you can access within the scope of the function
  • External parameter name The name of the parameter you have to provide when calling the function

It is written the following way:

func myFunction(externalParamterName internalParameterName: Int) {
    // internalParameterName is the internal name and as such
    // accessible within the scope of this function
}
myFunction(externalParameterName: 2)

Please note that as of Swift 2, the # shorthand does not work any more. I have just answered a question here where parameter names are explained in detail, also check out the link to Apple Swift docs. But keep in mind this is for Swift 2!

awesome! thank you