python函数中的形式参数和实际参数 [英] Formal and actual parameters in a function in python
本文介绍了python函数中的形式参数和实际参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对如何在递归函数中识别形式参数和实际参数感到困惑。例如,在此代码块中,获取数字的阶乘:
I'm kind of confused on how to identify the formal and actual parameters in a recursive function. For example in this block of code for getting the factorial of a number:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
factorial(n-1)是形式参数,因为它位于函数本身内部?还是它是实际参数,因为它为函数分配了一个值。另外, factorial(n)也是形式参数吗?
Is "factorial(n-1)" the formal parameter since it is inside the function itself? Or is it the actual parameter because it assigned a value for the function. Also, is the "factorial(n)" the formal parameter as well?
推荐答案
函数定义中包含形式参数,即参数。实际参数(即参数)在函数调用中。
所以 n
在这里:
def factorial(n):
是形式参数。
和 n-1
(或更确切地说,它的值计算为):
And n - 1
(or rather, the value it evaluates to) here:
return n * factorial(n-1)
是实际参数,即自变量。
Is an "actual parameter", i.e. an argument.
这篇关于python函数中的形式参数和实际参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文