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

Raphael Reiter
Raphael Reiter
6,820 Points

underscore in Swift 3 ?

Hello,

Can someone please explain to me what is the signification of the underscore in Swift 3?

example:

func move(_ direction: Direction)

1 Answer

David Papandrew
David Papandrew
8,386 Points

Hi Raphael,

See the code below and the comments. It should help illustrate. Try in Playgrounds so you can play around with it.

//Example 1: Single name parameter
//Function has a single name that works as external and internal name.

func sayHello(greeting: String) {
    print(greeting)
}

sayHello(greeting: "Hello")



//Example 2: External and internal named parameter
//Function has external name, yourGreeting and internal name, greeting

func sayHelloExternalName(yourGreeting greeting: String) {
    print(greeting) //we use internal name inside function
}

//We use external name outside the function
sayHelloExternalName(yourGreeting: "Hello")



//Example 3: Parameter with underscore
//Function has underscore for external name which means "no need to use an external name"

func sayHelloNoExternalName(_ greeting: String) {
    print(greeting) //internal name used
}

//Function just passes argument. No external name.
sayHelloNoExternalName("Hello")
Raphael Reiter
Raphael Reiter
6,820 Points

ok. very clear. thanks :-)