Subscribe Us

Return statement | Python Recursion | Python Language | 2023

 

return Statement

The return statement is used to return the value of the expression back to the main function.

 

Example:

def name(fname, mname, lname):
    return "Hello, " + fname + " " + mname + " " + lname

print(name("James", "Buchanan", "Barnes"))

 

Output:

            Hello James Buchanan Barnes


Python Recursion

We can let the function call itself, such a process is known as calling a function recursively in python.

 

Example:

def factorial(num): 
    if (num == 1 or num == 0):
        return 1
    else:
        return (num * factorial(num - 1)) 
  
# Driver Code 
num = 7; 
print("number: ",num)
print("Factorial: ",factorial(num))

 

Output:

number:  7
Factorial:  5040tr

Post a Comment

0 Comments