Single point return or multiple return?

What is the better choice: single point return or multiple return functions?


##---------------------------- exercise
def fizz_buzz(input):  # single point return
    if input % 3 == 0 and input % 5 == 0:
        result =  "fizzbuzz"
    if input % 3 == 0:
        result = "fizz"
    if input % 5 == 0:
        result =  "buzz"
    else:
        result =  input
    return result


def shizz_buzz(input): # Mosh way, the multiple return
    if input % 3 == 0 and input % 5 == 0:
        return "fizzbuzz"
    if input % 3 == 0:
        return "fizz"
    if input % 5 == 0:
        return "buzz"
    else:
        return input


print(fizz_buzz(7))
print(shizz_buzz(15))

Using multiple returns is better than the previous approach because it is:

  • Easy to understand.
  • Efficient, as it avoids checking unnecessary conditions.
1 Like

Thanks for your reply.

Kind regards,

Wullie