Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

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

2 Answers

Martin Wildfeuer
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