user1887639
user1887639

Reputation: 79

returning values from python functions

First off apologies if this has been asked before. I'm a newcomer to coding, as you'll soon see.

I've simplified what I'm trying to achieve below.

In essence, I would like to pass two variables into either functions below (fun1 or fun2 could be initiated first). Once either val1 or val2 has reached 0, I would like to return the alternate value. Obviously in the example below, val2 will always reach 0 and fun1 will be initiated first.

I was wondering if there's a way to return the value to blah? I understand that the below example will be creating some ridiculous loop between the two functions, but I've no idea how to accomplish what I'm after, without extensive if statements and a singular function.

Any help will be greatly appreciated.

    def fun1(val1, val2):
        val2 -= 1
        if val2 > 0:
            print "val2: ", val2
            fun2(val1, val2)
        else:
            print "val2 ended"
            return val1

    def fun2(val1, val2):
        val1 -= 1
        if val1 > 0:
            print "val1: ", val1
            fun1(val1, val2)
        else:
            print "val1 ended"
            return val2

    blah = fun1(10,8)
    print blah

Upvotes: 2

Views: 422

Answers (3)

Henry Keiter
Henry Keiter

Reputation: 17188

Quite easily! When you do your recursive calls (calling the other functions), just return their value instead of ignoring it. Like this:

def fun1(val1, val2):
    val2 -= 1
    if val2 > 0:
        return fun2(val1, val2) # RETURN the result!
    else:
        print "val2 ended"
        return val1

def fun2(val1, val2):
    val1 -= 1
    if val1 > 0:
        return fun1(val1, val2) # RETURN the result!
    else:
        print "val1 ended"
        return val2

This way, no matter which code path you take, you always return a result at each step to the next level above.

Upvotes: 2

Stephan
Stephan

Reputation: 18041

You were very close, you can do this recursively, what this does is that it will return the value that is returned by the function is called, so on and so forth until it decrements to -1, and then it will return the final number to blah

def fun1(val1, val2):
    val2 -= 1
    if val2 > 0:
        print "val2: ", val2
        return fun2(val1, val2)
    else:
        print "val2 ended"
        return val1

def fun2(val1, val2):
    val1 -= 1
    if val1 > 0:
        print "val1: ", val1
        return fun1(val1, val2)
    else:
        print "val1 ended"
        return val2

blah = fun1(10,8)
print blah

This will store the result in blah

Upvotes: 2

unutbu
unutbu

Reputation: 880997

Make sure you return fun2(...) and return fun1(...). Without the returns, Python functions return None by default.

def fun1(val1, val2):
    val2 -= 1
    if val2 > 0:
        print "val2: ", val2
        return fun2(val1, val2)
    else:
        print "val2 ended"
        return val1

def fun2(val1, val2):
    val1 -= 1
    if val1 > 0:
        print "val1: ", val1
        return fun1(val1, val2)
    else:
        print "val1 ended"
        return val2

blah = fun1(10,8)
print blah

Upvotes: 1

Related Questions