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)
阶乘(n-1)"是形式参数,因为它在函数本身内部吗?或者它是实际参数,因为它为函数分配了一个值.另外,阶乘(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)
是实参",即参数.
这篇关于python中函数的形参和实参的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python中函数的形参和实参


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01