Reputation: 6772
I'm trying to learn swift and wrap my brain around what's going on here. Any guidance at all would be greatly appreciated.
This example is from the swift tour
1. class Counter {
2. var count: Int = 0
3. func incrementBy(amount: Int, numberOfTimes times: Int) {
4. count += amount * times
5. }
6. }
7. var counter = Counter()
8. counter.incrementBy(2, numberOfTimes: 7)
question 1: On line three, why use 'numberOfTimes' instead of just 'times' on it's own?
question 2: On line eight, why do I need to pass the name of the parameter 'numberOfTimes' but not the name of the parameter 'amount'?
Upvotes: 0
Views: 72
Reputation: 72780
You can find all your answers about Function Parameter Names in the official documentation.
Specifically, you should read about External Parameter Names
Upvotes: 0
Reputation: 3991
1°) A parameter can have an external name and an internal name. External name will be used when calling the method and internal name will be used inside the method. So you can use expressive method like tableView(tableView:UITableView, numberOfRowInSection section:Int)
without having to use a long named parameter inside the function.
2°) Rules for methods : first parameters don't have to be named but the others needed to. This is for understanding matter.
Example : counter.incrementBy(2, 7)
; here you can understand why 2
is used because of incrementBy
but you can't understand what 7
is used for
Upvotes: 2