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 trialEmmanuel Darmon
6,115 PointsWhy name "width" visible when we call the function?
calculateArea(10, width: 12)
In the video, it is said that "the name length is invisible even though we've specified it up here, but width is" + "Don't worry about that now, we'll get to that in a second"... I maybe missed the explanations, but even in the next video I couldn't find the reason of that. Can you help?
2 Answers
Jhoan Arango
14,575 PointsUPDATE: For Swift 3.0 update please see end of this answer.
Hey :
Functions have parameters, and these parameters can have names:
Local Parameter name
- A local parameter name is used for the implementation of the function, so basically you can use this name in the scope of the function.
External Parameter name
- An external parameter name is used to label arguments passed to a function call, in other words, when you call a function.
By default, the first parameter omits it's external parameter name, and the second and subsequent use their local parameter name, as their external name.
For example.
// This function has 2 parameters
func someFunc(name: String, lastName: String){
print("Hello \(name) \(lastName)")
}
// When calling this function, it will look like this.
someFunc("John", lastName: "Smith")
Notice how the first parameters name is not being used. That's because it's omitting it. The second parameter was used, since it uses it's local parameter as external.
If you want to make the first parameter appear when you call the function, you have to specify it in the functions declaration.
For example:
// I am giving the first parameter an external name as "firstName"
func someFunc(firstName name: String, lastName: String){
print("Hello \(name) \(lastName)") // here we are using the local parameter name
}
// When calling the function it will look like this
someFunc(firstName: "John", lastName: "Smith")
Hopefully this quick explanation helps you understand what he is doing, and parameter names.
Good luck, if you have any questions please let me know.
With the upcoming release of Swift 3.0, there are some changes.
- External Parameter name is now called Argument Label
- Local Parameter name is now called Parameter Name
- You now have to implicitly ignore the Argument Label with _ , if you do not want to use it when calling the function. By default, parameters use their parameter name as their argument label.
Emmanuel Darmon
6,115 PointsActually the answer is in the following class: https://teamtreehouse.com/library/swift-20-functions/function-parameters/named-parameters
Emmanuel Darmon
6,115 PointsEmmanuel Darmon
6,115 PointsHelps a lot, thank you so much !