为什么Python返回None而不是value

为什么Python返回None而不是value,python,c++,algorithm,math,Python,C++,Algorithm,Math,又是一个愚蠢的问题。 我这里有一个非常简单的算法来计算最大公除法 我的C++片段看起来像这个< /P> int findGcd(int m, int n) { int r = m % n; if(r == 0) { return n; } else { m = n; n = r; findGcd(m, n); } } int main() { cout <&

又是一个愚蠢的问题。 我这里有一个非常简单的算法来计算最大公除法

我的C++片段看起来像这个< /P>

int findGcd(int m, int n)
{
    int r = m % n;

    if(r == 0)
    {
        return n;
    }

    else
    {
        m = n;
        n = r;
        findGcd(m, n);
    }
}

int main()
{
    cout << findGcd(13, 3);

    return 0;
}
它只返回NONE而不是1。我已经对它进行了调试,n确实存储了正确的值1,但仍然返回

我可以通过在else分支中递归调用函数时添加“return”来解决这个问题。
但这是为什么呢?

在这两种情况下,在递归调用中都需要一个
返回值

如果没有,在C++中,你有未定义的行为。在Python中,您可以得到
None

C++

Python:

def findGcd(m, n):
"""Calculates the greatest common divider """
    r = m % n

    if r == 0:
        return n

    else:
        m = n
        n = r
        return findGcd(m, n)
<>你通过增加编译器上的警告级别来捕捉C++中的这些问题。使用
g++-Wall
,我得到:

socc.cc:在函数“int findGcd(int,int)”中:
socc.cc:16:1:警告:控件达到非无效函数的末尾[-Wreturn类型]
}
^

>我不能在C++ +@ JuaPA.AcViLaGa中复制输出,因为在C++中,缺少的<>代码>返回/代码>会导致未定义的行为。我使用的是在线C++编译器。原来是s#t。很抱歉在这件事上浪费了你的时间。这场误会只是由于编撰者的意外行为造成的。谢谢你的帮助!注意,有些语言具有将隐式返回的返回语句,例如Scala。但是,你不应该假设,因为某些东西在C++中工作(或者在这种情况下,它没有)或者Scala,它将在Python中工作。
int findGcd(int m, int n)
{
    int r = m % n;

    if(r == 0)
    {
        return n;
    }

    else
    {
        m = n;
        n = r;
        return findGcd(m, n);
    }
}
def findGcd(m, n):
"""Calculates the greatest common divider """
    r = m % n

    if r == 0:
        return n

    else:
        m = n
        n = r
        return findGcd(m, n)