Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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! :)