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

What's the difference between local and external parameters?

I've watched the video a few times, and still don't understand the difference and the need for the extra label...

2 Answers

Nathan F.
Nathan F.
30,773 Points

You don't necessarily -need- the extra label names, but they can clear up confusion. And they don't have to be identical.

Compare these two functions.

func appendString(myString: String, withString s2: String) {
  return myString + s2
}

appendString("Hello", withString: "World")
func appendString(s1: String, s2: String) {
  return s1 + s2
}

appendString("Hello", "World")

In the first function, it's a lot more clear that the first parameter is the string I want to add on to, and that the second parameter is the thing I want to attach to the end of my primary string. In the second string--how do you know, exactly, that s2 will be added onto the end of s1? You can look at the implementation, or you might be able to reason that it's just common sense, but there's no guarantee it will be.

Or take a far more likely (and more ripe for confusion) example. Somewhere in the code, someone calls this function:

registerEmployee("Adam", "Nathaniel")

Which parameter is the employee's first name? All depending on the country and personal preferences, a name might be filed as Family Name, First Name or First Name Family Name. If the function were defined like this:

func registerEmployee(#lastName: String, #firstName: String) {
///do stuff
}

registerEmployee(lastName: "Adam", firstName: "Nathaniel")

It becomes clearer, right?

Thanks for the help! I think it would've been easier to understand if Amit had begun the explanation without using the label and parameter name as the same thing... (#). Anyways, I get it now. :)

Nathan F.
Nathan F.
30,773 Points

Amit talks about them here: https://teamtreehouse.com/library/functions-and-optionals/parameters-and-tuples/named-parameters

(By the way, Treehouse team, it'd be great if there were a way to jump back to the video these questions mention from the forums! :D )

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

There is a way, the link on the side of post goes to video. When I re-watched the video I didn't hear Amit talk about it, as in local vs. external.

Nathan F.
Nathan F.
30,773 Points

Oops! So there is. For some reason I always click the little post tags at the bottom with the Video icon, thinking it will take me there. Well, now I know.

Amit starts talking about it starting at about 1:59. It's very brief commentary. From the transcript:

Now, the parameters that we have right now are called local parameters,
because they are being used locally inside the function.
To create external parameter names, which will be seen 
when calling the function we have to actually give it specific names.
So in our case, the labels for our parameters would be exactly the same as the local parameter names.