Beginner question: argument labels

Hi everyone!

I'm working my way through Apple's Swift documentation and learning sessions. I've come across an exercise related to argument labels that has me stumped.

Here is the function code the task refers to:



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)



Now the task is to add an argument label to the function definition so when the function is called it reads as such:

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

In order to alter the label 'reds' in the function definition to 'withReds' I'd have to alter the references to that label in the function code - but the task says I shouldn't have to do that.

Am I missing a trick relating specifically to "with" that means I can change something in the function definition without the function code itself? The addition of "with" has me a bit stumped and I'm not sure what it refers to!

Any help would be incredibly appreciated 😊
In Swift, you can specify labels and parameter names separately:
Code Block
func score(withReds reds: Int, greens: Int, golds: Int) -> Int {
//...
}
let finalScore = score(withReds: 5, greens: 3, golds: 3)


Hi OOPer, thanks so much for your reply.

That was my first look at the problem, however when changing the argument label I was getting errors in the function code that the parameter name isn't correspond with the label in the function definition.

You're absolutely right that they should operate and behave separately, however the code didn't seem to behave as if that were the case!

Does my explanation above immediately make you think I'm doing something else that's obviously wrong for the code to behave in that way?
Sorry, I cannot reproduce the same error with my code.
(Actually, it shows Missing return in a function expected to return 'Int'. But you already know you need return statement(t) somewhere in the function.)

Thus, the result is clear enough. You are doing something wrong, or your Xcode is broken.
Please copy exactly the code you have tried and that fails.

maybe you have a upper/lowercase mismatch in the label.
Beginner question: argument labels
 
 
Q