Yang - 8 months ago 24

Python Question

inside the function "bigger()", it has returned a value--a or b.

so, why do still need "return"s before "bigger()"s in the function "median()"?

Here is the code:

`def bigger(a,b):`

if a > b:

return a

else:

return b

def biggest(a,b,c):

return bigger(a,bigger(b,c))

def median(a,b,c):

if a == biggest(a,b,c):

return bigger(b,c) #the function "bigger()" above has returned a value,

#so, why still need a 'return' before bigger(b,c)?

elif b == biggest(a,b,c):

return bigger(a,c)

else:

return bigger(a,b)

Answer

I'll try to explain with examples, maybe it is simpler.

You have defined your `bigger`

function, that returns a value, right?

```
def bigger(a,b):
if a > b:
return a
else:
return b
```

Now, you have defined a `biggest`

function, and you are calling `bigger`

from that function.

```
def biggest(a,b,c):
return bigger(a,bigger(b,c))
```

Now imagine the thousands of stuff that you could do with the value returned from `bigger`

.

You could use the **returned value** from `bigger`

anywhere within `biggest`

function. For example:

```
def biggest(a,b,c):
some_var = bigger(a,bigger(b,c))
return some_var + 10
```

Or

```
def biggest(a,b,c):
list_of_values = []
list_of_values.append(bigger(a,bigger(b,c)))
```

But as you wish to use the value returned from `bigger`

in the function that calls `biggest`

, you should return that again so that the caller of `biggest`

gets this value. Otherwise, it would get `None`

which basically tells the caller "No value returned."