Recursion
Recursion Functions
When a function calls itself is known as recursion. Recursion works like loop but sometimes it makes more sense to use recursion than loop. You can convert any loop to recursion.
A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition! Such a process is known as infinite iteration. The condition that is applied in any recursive function is known as base condition. A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop.
Overview of how recursive function works
1. Recursive function is called by some external code.
2. If the base condition is met then the program gives meaningful output and exits.
3. Otherwise, function does some required processing and then calls itself to continue recursion.
Here is an example of recursive function used to calculate factorial.
Example :
def fact(n):
if n == 0:
return 1
else:
return n * fact (n-1)
print (fact (0))
print (fact (5))
Output:
1
120
- A function that calls itself
- A function execution instance that calls another execution instance of the same function
- A class method that calls another class method
- An in-built method that is automatically called
- Recursive function can be replaced by a non-recursive function
- Recursive functions usually take more memory space than non-recursive function
- Recursive functions run faster than non-recursive function
- Recursion makes programs easier to understand
Fill in the line of the following Python code for calculating the factorial of a number.
def fact(num): if num == 0: return 1 else: return _____________________
- num*fact(num-1)
- (num-1)*(num-2)
- num*(num-1)
- fact(num)*fact(num-1)
What will be the output of the following Python code?
def test(i,j): if(i==0): return j else: return test(i-1,i+j) print(test(4,7))
- 13
- 7
- Infinite loop
- 17
What will be the output of the following Python code?
l=[] def convert(b): if(b==0): return l dig=b%2 l.append(dig) convert(b//2) convert(6) l.reverse() for i in l: print(i,end="")
- 011
- 110
- 3
- Infinite loop
- A recursive function that has two base cases
- A function where the recursive functions leads to an infinite loop
- A recursive function where the function doesn’t return anything and just prints the values
- A function where the recursive call is the last thing executed by the function
Observe the following Python code?
def a(n): if n == 0: return 0 else: return n*a(n - 1) def b(n, tot): if n == 0: return tot else: return b(n-2, tot-2)
- Both a() and b() aren’t tail recursive
- Both a() and b() are tail recursive
- b() is tail recursive but a() isn’t
- a() is tail recursive but b() isn’t
- Every recursive function must have a base case
- Infinite recursion can occur if the base case isn’t properly mentioned
- A recursive function makes the code easier to understand
- Every recursive function must have a return value
What will be the output of the following Python code?
def fun(n): if (n > 100): return n - 5 return fun(fun(n+11)); print(fun(45))
- 50
- 100
- 74
- Infinite loop
- Program gets into an infinite loop
- Program runs once
- Program runs n number of times where n is the argument given to the function
- An exception is thrown
- Making the code look clean
- A complex task can be broken into sub-problems
- Recursive calls take up less memory
- Sequence generation is easier than a nested iteration
- It’s easier to code some real-world problems using recursion than non-recursive equivalent
- Recursive functions are easy to debug
- Recursive calls take up a lot of memory
- Programs using recursion take longer time than their non-recursive equivalent
What will be the output of the following Python code?
def a(n): if n == 0: return 0 elif n == 1: return 1 else: return a(n-1)+a(n-2) for i in range(0,4): print(a(i),end=" ")
- 0 1 2 3
- An exception is thrown
- 0 1 1 2 3
- 0 1 1 2
- 9
- 10
- 11
- 12
- Subtracting two numbers
- Comparing two data values
- Providing output to the user
- Adding two numbers
- A function that calls itself
- A function that calls other functions
- Both (A) and (B)
- None of the above
- A function that calls other function.
- A function which calls itself.
- Both A and B
- None of the above
- Repeating a process in a loop
- Calling a function from within itself
- Using a loop to iterate over elements
- Breaking down a problem into smaller subproblems