81
func say(name:String, msg:String) {
    println("\(name) say \(msg)")
}

say("Henry","Hi,Swift") // error: missing argument label 'msg' in call

I need to use

say("Henry",msg:"Hi,Swift")

Why? If I put more than two variables in the function parameter list, I need to write the variable name instead of just the variable when I call this function.

It's really trouble, and I don't see any explanation in iBook Swift tutorial.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
henry4343
  • 3,871
  • 5
  • 22
  • 30

7 Answers7

72

One possible reason is that it is actually a method. Methods are very sneaky, they look just like regular functions, but they don't act the same way, let's look at this:

func funFunction(someArg: Int, someOtherArg: Int) {
    println("funFunction: \(someArg) : \(someOtherArg)")
}

// No external parameter
funFunction(1, 4)

func externalParamFunction(externalOne internalOne: Int, externalTwo internalTwo: Int) {
    println("externalParamFunction: \(internalOne) : \(internalTwo)")
}

// Requires external parameters
externalParamFunction(externalOne: 1, externalTwo: 4)

func externalInternalShared(#paramOne: Int, #paramTwo: Int) {
    println("externalInternalShared: \(paramOne) : \(paramTwo)")
}

// The '#' basically says, you want your internal and external names to be the same

// Note that there's been an update in Swift 2 and the above function would have to be written as:

func externalInternalShared(paramOne paramOne: Int, #paramTwo: Int) {
    print("externalInternalShared: \(paramOne) : \(paramTwo)")
}

externalInternalShared(paramOne: 1, paramTwo: 4)

Now here's the fun part, declare a function inside of a class and it's no longer a function ... it's a method

class SomeClass {
    func someClassFunctionWithParamOne(paramOne: Int, paramTwo: Int) {
        println("someClassFunction: \(paramOne) : \(paramTwo)")
    }
}

var someInstance = SomeClass()
someInstance.someClassFunctionWithParamOne(1, paramTwo: 4)

This is part of the design of behavior for methods

Apple Docs:

Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.

Notice the autocomplete: enter image description here

Logan
  • 52,262
  • 20
  • 99
  • 128
  • Funny how without providing the name for the second parameter in Playground seems to work beautifully! – user1107173 Oct 10 '14 at 04:56
  • 1
    @user1107173 - That's because it's out of the context of a class. If you put it inside a class, it would provide the name for the second parameter. Playground doesn't have anything to do with it :) – Logan Oct 10 '14 at 13:03
  • This is no longer true as of Swift 2.1. Functions and methods now share the exact same semantics for parameter labels, with the exception of special `init` methods. See [this answer](http://stackoverflow.com/a/34188996/1292061). – ravron Feb 12 '16 at 20:53
11

This is simply an influence of the Objective-C language. When calling a method, the first parameter of a method does not need to be explicitly labelled (as in Objective-C it is effectively 'labelled' by the name of the method). However all following parameters DO need a name to identify them. They may also take an (optional) local name for use inside the method itself (see Jiaaro's link in the comments above).

Ephemera
  • 8,672
  • 8
  • 44
  • 84
  • 2
    I feel like instead of saying "does not need to be explicitly labeled" we could say "cannot be labeled" because adding a label there will result in a compilation error. – sivabudh Jun 20 '14 at 06:15
8

Simple:

Wrong call function syntax's( its not same in c/c++/java/c#)

Incorrect:

say("Henry")

Correct:

say(name:"Henry")

PS: You must always! add "name function parameter" before value.

Fortran
  • 2,218
  • 2
  • 27
  • 33
  • Not 2015. That early Swift version the first label was ommited, just like in Objective-C in methods and functions did not need anyone. You are talking about a newer Swift version than OP. – Binarian Feb 05 '18 at 08:32
  • @iMalleus yeah) – Fortran Feb 05 '18 at 10:34
3

Swift 3.0 update:

In swift 3.0, methods with one param name per inputs are required to have that param name as part of the function call. So if you define the function like this

func say(name:String, msg:String) {
    print("\(name) say \(msg)")
}

Your function call will have to be like this

self.say(name: "Henry",msg: "Hi,Swift")

If you want to have English like readable function labels but do not want to change input param name, you can add the label in front of the parameter names, like this

func say(somethingBy name:String, whoIsActuallySaying msg:String) {
    print("\(name) say \(msg)")
}

Then calling it like this

self.say(somethingBy: "Henry",whoIsActuallySaying: "Hi,Swift")
Community
  • 1
  • 1
Fangming
  • 24,551
  • 6
  • 100
  • 90
1

This is a quirk in the compiler. Functions (which are not members of a class) and class methods have different default behavior with regards to named parameters. This is consistent with the behavior of named parameters in objective-C (but makes no sense for someone new to swift with no experience with objective-C).

Here's what the language reference has to say about named parameters for functions (specifically parameters where an external name for the parameter is not given, and the parameter does not have a default value)

However, these parameter names are only used within the body of the function itself, and cannot be used when calling the function. These kinds of parameter names are known as local parameter names, because they are only available for use within the function’s body.

For information about class methods, see Logan's answer.

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • It's actually not that simple. If you read further into the documentation, there are times when the internal name is automatically turned into an external name, from what I remember it's primarily within class definitions the 2nd and following tags are required by default. – David Berry Jun 05 '14 at 06:10
  • 4
    @David Agreed, my answer covers the case where the `func` is actually a function. Logan's answer covers the case where the `func` is actually a method. Personally, I don't like this design choice on Apple's part. The function/method calling syntax should be consistent, regardless of context. Programmers can use the `#` syntax to force the use of parameter names as a matter of style. – user3386109 Jun 05 '14 at 06:28
1

If you prefer to keep the calling code as intended, you can now use underscores to avoid the labels:

func say(_ name: String, _ msg: String) {
    print("\(name) say \(msg)")
}

say("Henry", "Hi,Swift")

...although the labels are useful for readability in many cases (such as this one--it's hard to tell what the arguments mean otherwise).

ggorlen
  • 44,755
  • 7
  • 76
  • 106
-1

Please find the small code for understanding in swift 3+.

func sumInt(a:Int,b:Int){
    print(a+b) // Displays 3 here
}

sumInt(a: 1, b: 2) // not like in other languages
Lokesh G
  • 831
  • 11
  • 19