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

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 :-)