Himanshu Jotwani
Himanshu Jotwani

Reputation: 406

returning values of function in python

Why do I have to return function in the else case? Can't I just apply the defined function because I have to only return the value of b and store it?

def gcd_a(a,b):

    if a==0:
        return b
    else:
        gcd_a(b%a,a)

Upvotes: 3

Views: 2202

Answers (4)

rawwar
rawwar

Reputation: 4992

In the else block, if you don't return the function call, the outer function returns None because python interpreter just runs whatever is returned by the gcd function.

Let's assume the following code:

def func():
    10

In that function, it just runs 10, but it doesn't mean that there is some return from the function.

Upvotes: 1

Nishant
Nishant

Reputation: 21914

IMHO, the problem is in the way you are thinking about function which has to be cleared. The fact that you are having this doubt in a recursive function call is incidental because you are calling the same function again and again with different arguments causing different branches to be executed.

How will a function that doesn't return anything in any of its branches be helpful to its caller? In your case, what would happen if your function's caller calls it with an argument that hits your else block? It will return nothing and this won't help the caller!

Now in your recursive case, if your caller calls the function with an argument that hits the if block, then it would work as expected. However, if it hits the else block, then it would become a caller and call the same function again. For simplicity, let us assume that this time it hits the if condition and returns something to its caller. However, will that reach the original caller that initiated everything? The answer is no because you are not returning it to him!

Most of the times you would need a return for every branch in a function unless you are doing it on purpose for a side effect.

Upvotes: 1

user3483203
user3483203

Reputation: 51165

I think the main concept you are missing is that in order to get the result of a recursive function (or any function for that matter), the function that you access the result of must return the value*

Right now, when you call gcd_a, a recursive call to gcd_a will eventually return a value, however it will be lost, since the function that you are accessing the result of does not return a value.

To show you this is true, let's add in a log statement that prints when the function is returning:

def gcd_a(a,b):
    if a==0:
      print('finally returning!', b)
      return b
    else:
        gcd_a(b%a,a)

Now if we call:

print(gcd_a(10, 99))

We get the following output:

finally returning! 1
None

Somewhere, a recursive call to gcd_a found your condition to be True, and returned 1, however, this result is not printed, because it is not returned by your call to gcd_a


* Unless you do something strange like updating a global variable, etc...

Upvotes: 3

MxLDevs
MxLDevs

Reputation: 19506

If your function finishes without returning anything, it will implicitly return None.

def double(val):
   val * 2

value = double(10)
print(value)

Which is fine, except it doesn't have a return statement, and so all you get is None

The same applies to recursive functions: you can do all the recursive function calls you want, but if you don't actually return the result of the function, then it'll return None.

Upvotes: 1

Related Questions