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 Swift Functions and Optionals Optionals Exercise: isDivisible function

Adrien Yvon
Adrien Yvon
3,079 Points

Is "Else" necessary in the function?

Hi I was wondering the utility of else in the function, I didn't use it and it worked pretty good.

Here's the code :

func isDivisible (Number1 number1: Int, Number2 number2: Int) -> Bool? {

if number1 % number2 == 0 {
    return true }

    return nil

}

Thank you !

2 Answers

Adrien Yvon
Adrien Yvon
3,079 Points

Hi Richard thank you for the explanation. I think I got it.

Richard Lu
Richard Lu
20,185 Points

Hi Adrien,

In your case, else is never needed because if that if statement is true, then the outer return statement is never reached. If you'd like to experiment, try this out.

func testFunction(condition: Bool) {
   if condition {
      print("hey it's true!")
      return
   }
   print("hey it's not true")    // this line never gets touched
}

testFunction(true)  // prints "hey it's true!"

Best of luck and Happy Coding! :)