Argument label and parameter name

in the playground 9.1 im learning Xcode. at lesson 10, i can't understand argument label and parameter name.

could please explain them to me VERY easily.

for example i didn't understand the Exercise: argument label below:

what should i do now?

thank you...



/:
## Exercise: Argument Label
Functions and their arguments should be named so that they read like a clear instruction when they’re called. To make this easier, you can give parameters two names - an _argument label_ to be used when calling the function and a _parameter name_ to be used within the function’s body.
*/
func score(reds: Int, greens: Int, golds: Int) -> Int {
    let pointsPerRed = 5
    let pointsPerGreen = 10
    let pointsPerGold = 30
   
    let redScore = reds * pointsPerRed
    let greenScore = greens * pointsPerGreen
    let goldScore = golds * pointsPerGold
   
    return redScore + greenScore + goldScore
}
let finalScore = score(reds: 5, greens: 3, golds: 3)
/:
- callout(Exercise): Add an argument label to the function definition so it reads like this when you call it:\
`let finalScore = score(withReds: 5, greens: 3, golds: 3)`
*/
//: [Previous](@previous)  |  page 16 of 17  |  [Next: Exercise: No Argument Label](@next)

You should read the Swift documentation here:


https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html


especially under the heading "Function Argument Labels and Parameter Names".


The general idea is that it's desirable to use a different name for a parameter in the place where it's called, vs. its use within the function. For example, you may wish to allow callers of a function to write something "nice" like this:


     let sum = computeSum (of: aaa, and: bbb)// looks good!


But in your implementation of the function, calling the parameters "of" and "and" could be hard to understand:


func computeSum (of: Int, and: Int) -> Int {
     return of + and // doesn't read well!
}


Instead, you can define different "local" names for the parameters:


func computeSum (of firstValue: Int, and secondValue: Int) -> Int {
     return firstValue + secondValue // now it makes sense
}

thanks

but could you please explain this below to me?

/:
## Exercise: Argument Label
Functions and their arguments should be named so that they read like a clear instruction when they’re called. To make this easier, you can give parameters two names - an _argument label_ to be used when calling the function and a _parameter name_ to be used within the function’s body.
*/



func score(reds: Int, greens: Int, golds: Int) -> Int {    //please show me which of them are "parameteræ, "parameter" "name", "argument", "argument label"
    let pointsPerRed = 5
    let pointsPerGreen = 10
    let pointsPerGold = 30

    let redScore = reds * pointsPerRed
    let greenScore = greens * pointsPerGreen
    let goldScore = golds * pointsPerGold

    return redScore + greenScore + goldScore
}
let finalScore = score(reds: 5, greens: 3, golds: 3)
/:


(Exercise): Add an argument label to the function definition so it reads like this when you call it:\
`let finalScore = score(withReds: 5, greens: 3, golds: 3)`            //please tell me what should I write for this Exercise?
*/


//it means, I just have to change      reds: Int     to      withReds: Int    everywhere  ?


//Thanks a million

You should write the func (what is called its signature) as:


func score(withReds reds: Int, greens: Int, golds: Int) -> Int {


you call it as:


let finalScore = score(withReds: 5, greens: 3, golds: 3)


If you write the func as:

func score(withReds reds: Int, withGreens greens: Int, withGolds golds: Int) -> Int {

you call it as:

let finalScore = score(withReds: 5, withGreens: 3, withGolds: 3)


If you didn't want to see parameter names for greens and golds, you should write

func score(withReds reds: Int, _ greens: Int, _ golds: Int) -> Int {

you call it as:

let finalScore = score(withReds: 5, 3, 3)


Which is clearly very uneasy to use and prone to errors

If you don’t specify an argument label. Your parameter name is your argument label.

func score(reds: Int, greens: Int, golds: Int) -> Int {

In this case, reds, greens, and golds are not given argument labels, so those parameter names (reds, greens, and golds) are their argument labels.

Argument label and parameter name
 
 
Q