Multiple return statements

Screen Link: <!-- Enter URL of the screen below–>

def sum_or_difference(a, b, return_sum=True):
    if return_sum:
        return a + b
    else:
        return a - b

print(sum_or_difference(10, 5, return_sum=True))
print(sum_or_difference(10, 5, return_sum=False))

I don’t understand what “return_sum=True” is and how we can use this own function for…
Do we need to use return_sum=True or return_sum=False as parameter/

Could anyone please help with this…?

1 Like

Firstly, let’s understand what the function does; it returns a sum or difference of two numbers. The function has 3 parameters named a, b and return_sum; which has a default argument of True

This is used for checking the if conditional statement. See that the same parameter is used in the if statement

If the return_sum=TRUE, then the first block of if statement will get executed, which is, it will return the sum of a and b.

if TRUE:
    return a + b

If you want the difference of the numbers, then you will set the return_sum=False

if TRUE:
    return a+b
else:
   return a - b

Since, the return_sum is set to FALSE, the first block of code won’t get executed, it will move to the else block, which will return you the difference.

If you still didn’t understand, feel free to ask, I will be happy to help you.

2 Likes

Hi statement,
Thank you for your answer!

So… after creating this function,

if I want to use this function to get sum, I set the 3rd parameter as True;
print(sum_or_difference(10, 5, return_sum=True))

if I want to use this function to get difference, then I set the 3rd parameter as False;
print(sum_or_difference(10, 5, return_sum=False))

Is this correct??

1 Like

Yes, you are absolutely correct!

Thank you very much!
I understand perfectly now and this is useful function! :kissing_closed_eyes:

1 Like