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
chrisverra
3,760 Pointsi have to give the second parameter name to function?
It only works like this for me:
func calculateArea(height: Int, width: Int) -> Int{
let area = height * width
return area
}
calculateArea(5, width: 7)
i have to say width, i cant say width + height, that gives me error.
howcome it has to be this way?
3 Answers
Jhoan Arango
14,575 PointsHello Chris:
I believe your question is about naming your parameters. When creating a function, you can give its parameters names, so that when calling the function is more readable.
They consist of Local, and External names:
Local Parameter Names : Are used for the implementation of the function, so you can only use these names locally, meaning with in the function's body.
External Parameter Names : Are used to label the arguments passed into a function.
By default the first parameter omits it's external name, but the subsequent parameters use their local parameter name as their external name.
Here is an example of a function
func sayHello(to person: String, and anotherPerson: String) {
print("Hello \(person) and \(anotherPerson)")
}
// Calling the function
sayHello(to: "Chris", and: "John")
// Prints "Hello Chris and John.
Hope this helps you understand a bit more.
Good luck
chrisverra
3,760 PointsHi John,
I did understand this part, but thanks for clearing it out even more.
The thing i did not understand is when i call the function like this:
// note that i removed the parameter names
func sayHello( person: String, anotherPerson: String) {
print("Hello \(person) and \(anotherPerson)")
}
sayHello("Chris", anotherPerson: "John") // howcome I HAVE to give the name of the second paremeter
// and howcome I am NOT even allowed to also give the name of the first parameter
Jhoan Arango
14,575 PointsChris:
Notice how in my explanation it says that the first parameter omits its external parameter name.
func sayHello(person: String, anotherPerson: String) {}
// calling this function
sayHello("Chris", anotherPerson: "Jhon")
Since we didn't give the first parameter an external name, then when calling the function, that name will not appear. The second parameter name uses its local name AS the external parameter name as well, hence the reason why it appears when calling the function.
func sayHello(person: String, _ anotherPerson: String) {}
// Calling function
sayHello("Chris", "Jhon")
If we use _ before the name, it will then ignore the external parameter name when calling the function.
For a much better explanation, please click here and scroll down to look for " Function Parameter Name.
Good luck