Swift: difference between function calls

I am new to Swift programming. I can not understand the following code. In the following code to call

  • incrementBy method : I need to type numberOfTimes in the second argument. But,
  • sampleFunc method : I do not need to enter lastName in the second argument.

    class Counter {
    var count: Int = 0
    func incrementBy(amount: Int, numberOfTimes : Int) {
        count += amount * numberOfTimes
    }
    }
    
    var counter = Counter()
    counter.incrementBy(1, numberOfTimes : 3)
    print(counter.count)
    
    func sampleFunc(firstName : String, lastName : String){
    print("Hello \(firstName) \(lastName)")
    }
    
    sampleFunc("Abel", "Rosnoski")
    

Why is there a subtle difference? Please explain.

+4
source share
3 answers

( struct enum) , . , , . .

incrementBy(amount: Int, numberOfTimes : Int) , . , numberOfTimes, . :

counter.incrementBy(1, numberOfTimes : 3)

func sampleFunc(firstName : String, lastName : String) . . :

sampleFunc("Abel", "Rosnoski")

.

: User3441734 ( ) " . init , _". .

+5

@NSNoob , , :

func sampleFunc(firstName : String, lastName : String) {}
sampleFunc("First", lastName: "Last")

:

func sampleFunc(firstName : String, _ lastName : String) {}
sampleFunc("First", "Last")

:

func sampleFunc(firstName firstName : String, lastName : String) {}
sampleFunc(firstName: "First", lastName: "Last")

:

func sampleFunc(firstName firstName : String, lastName : String = "Last") {}
sampleFunc(firstName: "First")

,

+4
func a(param1:Double, param2:Double) -> Double {
    return param1*param2;
}

func a2(p1 param1:Double,p2 param2:Double) -> Double {
    return param1*param2;
}

As I understood and did some tests on the playground, you need to use parameter names if you do not define label labels.

var c = a(3.2,2.3)
c //gives error

var b = a(param1:3.2,param2:2.3);
b

var d = a2(p1:3.2,p2:2.3);
d

var e = a2(param1:3.2,param2:2.3)
e //gives error
0
source

Source: https://habr.com/ru/post/1614229/


All Articles