iOS Functions in Swift Functions in Swift Functions in Code

dakota ferrell
dakota ferrell
1,454 Points

if statement in functions

How do if statements work in functions I was messing around with this code and it will not run I dont know what is wrong? Is it because I cant use if statements on arguments inside of a function or is it for a different reason?

func greet (Name: String, Day: String) {
    let greetings = "hi " + Name + "today is " + Day
    if Day = Firday {
    print("Party time")
    }
    print(greetings)
}
greet(Name: "Dakota", Day: "Friday")

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,626 Points

Hi there,

To test for equality, use double equals == and if you want to see if a variable is equal to a string, use "" around the string.

if Day = Firday

becomes

if Day == "Friday"

Lastly, you probably shouldn't use capital initials for Day or Name but that's semantics.

Steve.

dakota ferrell
dakota ferrell
1,454 Points

Thanks but how would I get Party time to print to the screen because it will not print to the bottom consol

Steve Hunter
Steve Hunter
Treehouse Moderator 57,626 Points

Does the greeting print out?

Can you posts your amended code and I'll have a look.

Steve.

dakota ferrell
dakota ferrell
1,454 Points

Yeah greetings prints out but party time does not

func greet (Name: String, Day: String) {
    let greetings = "hi " + Name + "today is " + Day
    if Day == "firday" {
        print("Party time")
    }
    print(greetings)
}
greet(Name: "Dakota", Day: "friday")
Steve Hunter
Steve Hunter
Treehouse Moderator 57,626 Points

Typo - you are comparing firday to friday - correct one of them! :wink: