Recursive function and Scope of Variables
This post explains about recursive functions with examples and scope of variables in python. Scope of variables can be local scope and global scope.
Let us have a quick glimpse on what is function?
A function is a block or package of organized , reusable lines of code for later execution.
Functions provide modularity to the script by breaking huge codes in small modules.
This helps to avoid repetition and increases re-usability.
Python provides various built-in functions like int(), sqrt(), list(), type() etc.
We can also create own functions in python, this is termed as user defined functions.
In python we have already explained how to define function and call a function.
Such functions defined by user are called as user defined functions.
Generally, function call is mention in main() and after executing this statement control is shifted to actual function definition.
When a function is called by itself in its own definition is termed as recursive function and the process is called as recursion.
Python stop calling recursive function after 1000 calls by default. If we provide value as 1000 it will show “RuntimeError: maximum recursion depth exceeded in comparison” as it will move from 0 to 999
Working of recursive function
- Recursive function is called from main().
- If the condition is met then the script executes statements and exits .
- Else, function performs required processing and then calls itself to continue recursion
""" Author : ITVoyagers (itvoyagers.in) Date :8th September 2020 Description : Program to find factorial using recursive function """ def facto(num): if num == 1: return 1 else: return (num * facto(num-1)) print("Output of factorial using recursive function") print("-------------------------------------------------") n=int(input("Enter a number")) if n<0: print("Factorial does not exists for negative number") elif n==0: print("The factorial of 0 is 1") else: print("The factorial of",n,"is",facto(n))
""" Author : ITVoyagers (itvoyagers.in) Date :8th September 2020 Description : Program to find Fibonacci using recursive function """ def fibo(val): if val == 1 : return 0 elif val == 2: return 1 else: return (fibo(val-1)+fibo(val-2)) print("Output of fibonacci series using recursive function") print("-------------------------------------------------") n=int(input("Enter a number greater than 1 ")) for i in range(1,n): print(fibo(i))
Scope of Variables
The scope of variable can be defined by the area of script where the variable is accessible.
The variables can be accessible either inside function body or outside function body.
Scope of variables can be classified as local scope and global scope and variables accessible in that scopes are classified as local variable and global variable respectively.
Variables declared inside a function body are accessible only that function and not outside the function are called as local variables.
Such variables are present in local scope.
Variables declared outside function body are accessible throughout that function body as well as outside the function are called as global variables.
Such variables are present in global scope.
""" Author : ITVoyagers (itvoyagers.in) Date :8th September 2020 Description : Program to show local variables and global variables """ total=0 #total is global variable in global scope def add_me(a,b): #We can use both local and global variables inside function body total=a+b #a and b are local varibles in local scope print("Total value inside function is",total) return total print("Output of local and global variables") print("---------------------------------------") x=add_me(10,20) #x stores the value returned by funtion total = x+5 #total is global variable in global scope print("Total value outside function is",total)
Note: In above example along with local and global variables we have also seen how the function returns value and how that value can be used outside function definition.
For other python basics related posts:
For other advanced python related posts: