Why do you need to put the label "day:" but not "name:" in my Swift function program? -


i started learn swift , took official book on swift read. don't understand particular idea behind first example on functions.

this example code looks like:

  func greet(name: string, day: string) -> string {         return "hello \(name), today \(day)."     }     greet("bob", day: "tuesday") 

which gives correct output: "hello bob, today tuesday."


i don't understand why code has "day:" in:

greet("bob", day: "tuesday")

i mean, the issue is, if has have "day: tuesday", why don't have "name: bob", instead have "bob".



things i've replaced** greet("bob", day: "tuesday") with:

  1. greet(name: "bob", day: "tuesday")

    error: extraneous argument label 'name:' in call.

  2. greet(name: "bob", day: "tuesday")

    error: missing argument label 'day:' in call.

that's because default, name of first parameter of method (or function since swift 2) must not written when call method/function.

that's because generally, included in name of function, in case might have :

greetname(name:string, day:string) -> string 

so can call :

greetname("bob", day:"tuesday")   

if absolutely wish first parameter require name, can in method/function definition :

greet(name name:string, day:"tuesday") 

what doing here specifying external parameter name parameter. than, in code, call :

greet(name:"bob", day:"tuesday") 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -