Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Functions in Swift Functions in Swift Functions in Code

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

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.

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

Does the greeting print out?

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

Steve.

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")

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