user523234
user523234

Reputation: 14834

Methods in classes

These two snippets produced the same result. What is numberOfTimes (method?) How do you implement numberOfTimes?

class Counter
{
    var count: Int = 0
    func incrementBy(amount: Int, numberOfTimes times: Int)
    {
        count += amount * times
    }

}
var counter = Counter()
counter.incrementBy(2, numberOfTimes: 7)


class Counter
{
    var count: Int = 0
    func incrementBy(amount: Int, times: Int)
    {
        count += amount * times
    }
}
var counter = Counter()
counter.incrementBy(2, times: 7)

Upvotes: 0

Views: 48

Answers (3)

Gabriele Petronella
Gabriele Petronella

Reputation: 108121

I'm not sure I understand the question correctly, but numberOfTimes is the external parameter name for the times argument.

The only difference is that in one case you have to call the method as

counter.incrementBy(2, numberOfTimes: 7)

whereas if you leave it out, the local name is automatically used also as external

counter.incrementBy(2, times: 7)

More on the subject in the official docs.

Upvotes: 2

Antonio
Antonio

Reputation: 72760

That's an external parameter name, and it identifies the name you use when calling the function, whereas the local parameter name (times in your code) is how is the parameter is referenced from within the function.

Read more about Function Parameter Names

Upvotes: 1

Ideasthete
Ideasthete

Reputation: 1541

numberOfTimes is not a method, it is an external parameter name. Times is an internal parameter name. Check out this reference for more information.

Upvotes: 2

Related Questions