递归函数中的返回 [英] Return in Recursive Function

查看:76
本文介绍了递归函数中的返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习python(v3.2.3),遇到一个关于这个函数中return的奇怪问题:

I have just started learning python (v3.2.3) and have encountered an odd problem about the return in this function:

def test(x):
    if x > 9 :
        test(x - 10)
    else:
        print('real value',x)
        return x

x = int(input())
y = test(x)
print('this should be real value',y)

当我运行它时,我得到:

When I run it, I get:

45
real value 5
this should be real value None

但我期望:

45
real value 5
this should be real value 5

我尝试在 if 之外添加 return x 并且我得到了默认输入值.谁能解释一下 return 是如何工作的?

I tried adding return x outside of if and I got the default input value. Can anyone please explain how return works?

推荐答案

您调用 test(45).这将测试 45 >9,这是真的,所以它调用 test(35) (45 - 10),而不返回它的结果.同样的事情发生在 test(25)test(15) 上,直到最后 test(5) 被调用.

You invoke test(45). This tests whether 45 > 9, which is true, so it invokes test(35) (45 - 10), without returning its result. The same thing happens with test(25) and test(15), until finally test(5) is invoked.

这会打印真实值 5",然后返回 5.但是从函数返回结果总是 将其返回给该函数的直接调用者.它不会通过几次调用立即跳出;毕竟,调用者可能想在返回一些东西给它的调用者之前对返回的结果做一些事情.但在这种情况下,只有 test(5) 会返回任何内容;所有其他人调用 test(x - 10),等待它返回,忽略它返回的任何内容,然后(隐式)返回 None.由于最外层的调用 test(45) 是这些情况之一,您得到的是 None.

This prints 'real value 5', and then returns 5. But returning a result from a function always returns it to the direct caller of this function. It doesn't jump immediately out through several calls; after all, the caller might want to do something with the returned result before returning something to it's caller. In this case though, only test(5) returns anything at all; all the others call test(x - 10), wait for that to return, ignore whatever it does return, and then (implicitly) return None. Since the outermost invocation test(45) is one of these cases, what you get is None.

这是对所发生情况的可视化尝试:

Here's an attempt at a visualisation of what happens:

test(45):
| test(35):
| | test(25):
| | | test(15):
| | | | test(5):
| | | | | print('real value',5)
| | | | | return 5 to test(15)
| | | | return None to test(25)
| | | return None to test(35)
| | return None to test(45)
| return None

您没有在解释器中调用 test(5)test(5) 是从另一个函数调用内部调用的.因此,test(5) 的返回将转到那个函数调用.这是一个调用自身的函数这一事实完全无关.如果您的代码如下所示,您将获得完全相同的结果:

You didn't call test(5) in the interpreter, test(5) was called from inside another function call. So the return from test(5) goes to that function call. The fact that this is a function calling itself is completely irrelevant. You'd get exactly the same results if your code looked like this:

def test45(x):
    if x > 9 :
        test35(x - 10)
    else:
        print('real value',x)
        return x

def test35(x):
    if x > 9 :
        test25(x - 10)
    else:
        print('real value',x)
        return x

def test25(x):
    if x > 9 :
        test15(x - 10)
    else:
        print('real value',x)
        return x

def test15(x):
    if x > 9 :
        test5(x - 10)
    else:
        print('real value',x)
        return x

def test5(x):
    if x > 9 :
        print 'No more tests :('
    else:
        print('real value',x)
        return x

您使用 'x=45' 调用的 test(x) 函数与调用 test45(45) 相同.我希望你能明白为什么很明显 None 在不涉及递归时应该返回.好吧,当涉及递归时,没有任何变化.return 语句既不知道也不关心它是否从递归调用的函数返回,它在任何一种情况下的行为都完全相同.

The test(x) function you call with 'x=45' is same as calling test45(45). I hope you can see why it's obvious that None should be returned when recursion isn't involved. Well, when recursion is involved, nothing changes. The return statement neither knows nor cares whether it's returning from a recursively invoked function, it behaves exactly the same way in either case.

事实上,递归根本不是什么特别"的东西;它的行为方式与普通函数调用完全相同.您通过参数从调用您的事物接收信息,并通过返回将信息返回给调用您的事物.如果你没有返回任何东西(可能只在 if 的一个分支中),那么 None 将返回给你的调用者,无论你是否调用了任何其他函数那个分支,不管如果你确实调用了一些东西,那个函数可能会返回什么,不管你调用的函数是否恰好是你在里面的那个函数.

In fact, recursion isn't anything "special" at all; it behaves exactly the same way as ordinary function calls. You receive information from the thing that called you via arguments, and you return information to the thing that called you by returning. If you don't return something (perhaps only in one arm of an if), then None will be returned to your caller, regardless of whether you call any other function in that branch, regardless of what that function might return if you do call something, and regardless of whether the function you call happens to be the same function you're inside.

这篇关于递归函数中的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆