4

I am novice in Swift programming. I am not able to understand following code. In following code to call

  1. incrementBy method: I must have to type numberOfTimes in second argument. But,
  2. sampleFunc method: I must not have to type lastName in 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 subtle difference? Please explain.

Umang Kothari
  • 3,674
  • 27
  • 36
  • You want lastName in sampleFunc as optional value, right? – Viral Savaj Nov 03 '15 at 07:09
  • No I think he is asking that when you have to use "Named parameter" and when you can make do without the named parameter by just giving the value. Is that right @umangKothari? – NSNoob Nov 03 '15 at 07:10
  • It gives error as "Missed argument lastName" when compile like this `sampleFunc("Abel", "Rosnoski")` So, you need to use your second argument name like this `sampleFunc("Abel", lastName: "Rosnoski")` – Vijay Nov 03 '15 at 07:14
  • 1
    Possible duplicate of [Why does a function call require the parameter name in Swift?](http://stackoverflow.com/questions/24045890/why-does-a-function-call-require-the-parameter-name-in-swift) – Dániel Nagy Nov 03 '15 at 07:24

3 Answers3

5

When encapsulated in a class (or struct or enum), the first parameter name of a method is not included externally, while all following parameter names are included externally. The parameters which are not included externally, you don't have to give names for them when calling the method. But for externally included ones you have to.

Your method incrementBy(amount: Int, numberOfTimes : Int) is encapsulated in your class that's why the second parameter is automatically externally included. Which means you have to type numberOfTimes before providing value to parameter. That's why you must call it like:

counter.incrementBy(1, numberOfTimes : 3)

Your second method func sampleFunc(firstName : String, lastName : String) is not enclosed in your class. That's why you don't have to provide name for its parameters. Which is why you can call it like:

sampleFunc("Abel", "Rosnoski")

You can read more details here.

Edit: As User3441734 kindly pointed out (And I quote) "There is one exception. init with parameters always requires named parameter, if the external name is not specified as _ ". You can read more about Customizing initialization here.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • there is one exception. init with parameters always requires named parameter, if the external name is not specified as _ – user3441734 Nov 03 '15 at 08:37
4

@NSNoob made a nice explanation, I just added some examples to understand power of function calls:

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

You can remove naming second param like this:

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

If you want to add naming for all params:

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

If you want to make default value for param:

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

Hope that helps

katleta3000
  • 2,484
  • 1
  • 18
  • 23
  • This is not remotely explaining the concept of externally included parameters or the effect of a method being enclosed in the class. You are simply assuming that method is outside the class. – NSNoob Nov 03 '15 at 07:23
  • Yeah, I wrote already, that you explained well. It's not necessary to write the one thing twice. I just added some value information – katleta3000 Nov 03 '15 at 07:24
  • Also I would like to note that in the signature `func sampleFunc(firstName firstName : String, lastName : String)` the external and internal parameter name `firstName` is same so you don't have to type it twice. Just put a # before it. Something like this: `func sampleFunc(#firstName : String, lastName : String)` – NSNoob Nov 03 '15 at 07:26
  • 1
    # has been removed from Swift. it doesn't compile in `Swift 2.1` – katleta3000 Nov 03 '15 at 07:27
0
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 done some trials in playground, you have to use the parameter names if you don't define argument 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