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)
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?
解决方案
A formal parameter, i.e. a parameter, is in the function definition. An actual parameter, i.e. an argument, is in a function call.
So n here:
def factorial(n):
Is a formal parameter.
And n - 1 (or rather, the value it evaluates to) here:
return n * factorial(n-1)
Is an "actual parameter", i.e. an argument.