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 Functions Syntax and Parameters

Tammarra McPhaul
Tammarra McPhaul
640 Points

I am having trouble with task 3 on the parameters.swift challenge. How do I call the function?

Am I missing something?

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

greeting(person: "Tom")

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Tammarra:

You are doing great. You are calling the function the right way, but there is ONE small problem that can be solved fast, but I will explain so that you can understand better.

The function has two parameter names, the external parameter name and the local parameter name. The local parameter name is used in the implementation of the function, and the external is used when calling the function. Right now this function only has a “local” parameter name, therefore you don’t have an external name to use when calling the function.

How do I know it does not have an external name ? Because this is how it would look like if it did.

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

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

There are some new changes in Swift 2.0 and that’s why I gave you both examples.

Now on the function below, since we don’t have an external parameter name, then we don’t use it when calling the function.

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

greeting("Tom") // No external Parameter 

if it did have the external parameter name then we would call it like this

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

greeting(person: "Tom")

Hope you understand, if need a better explanation let me know..

Jhoan