Formal and actual parameters in a function in python

Viewed 27794

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?

5 Answers

when a function is defined, it may have some parameters. These parameters are useful to receive values from outside of the function. They are called 'formal arguments'. When we call the function, we should pass data or values to the function. These values are called 'actual arguments'. In the following code, 'a' and 'b' are formal arguments and 'x' and 'y' are actual arguments.

def sum(a, b): #a, b are formal arguments
    c = a+b 
    print(c)

#call the function 
x = 10; y = 15
sum(x, y) #x, y are actual arguments
Related