innovativedir
innovativedir

Reputation: 11

Swift Function with multiple parameters

I can't seem to figure out whats the issue here,

I have a swift code and I need it to gather multiple parameters. Please help:

func myBioData(myNameIs, myAgeIs) {
   println("Hello, my name is (myNameIs) and I am (myAgeIs) years old");
}

Upvotes: 1

Views: 4330

Answers (1)

Nick Ganguly
Nick Ganguly

Reputation: 163

There are multiple things you are doing wrong within your code here. To start off here is the right code and I will dissect it for you

func myBioData(myNameIs : String, var myAgeIs : Int) {
    println("Hello, my name is \(myNameIs) and I am \(myAgeIs) years old")
}
myBioData("Nick", 26)

First of all when you declare a parameter in Swift you need to explicitly state the type of the variable so you need to do something like this

(myNameIs: String)

Second thing to notice is that in Swift if you do not explicitly say that it's a variable that you are passing in Swift assumes that you are passing in a constant. Depending on what you are trying to do it may make a difference so I just added one as a constant and other as a variable to simply show you

Lastly when you are trying to add variables between a println you need to add a "\" before placing the variable/constant name so the syntax would be (myNameIs) would be right. Also, you should not put a semicolon at the end of the statement

Upvotes: 2

Related Questions