Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

XII CS - Chapter - 2 - Recursion-Notes

Recursion is a process where a function calls itself during its execution. This leads to recursive calls. For recursion to terminate, a base condition must be specified. Recursion allows for elegant code that is often more readable but can be less efficient due to increased function calls and potential for running out of memory. A recursive example prints numbers from 5 to 9 by calling itself with incremented values until the base case of the parameter reaching 10 is met.

Uploaded by

Bhakti Saraswat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

XII CS - Chapter - 2 - Recursion-Notes

Recursion is a process where a function calls itself during its execution. This leads to recursive calls. For recursion to terminate, a base condition must be specified. Recursion allows for elegant code that is often more readable but can be less efficient due to increased function calls and potential for running out of memory. A recursive example prints numbers from 5 to 9 by calling itself with incremented values until the base case of the parameter reaching 10 is met.

Uploaded by

Bhakti Saraswat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

RECURSION

Definition : Recursion is the process in which a function calls a copy


of itself .The function which calls itself is called recursive function, and
such function calls are called recursive calls.

Note : Condition must be specified to stop recursion; otherwise it will lead to


an infinite process.

Advantages of using Recursion:

i. Recursion is more elegant and requires a lesser number of variables which


makes the program short and clean.
ii. Recursion can lead to more readable and efficient algorithm descriptions

Disadvantages of using Recursion:

i. It slowing down execution time.

ii. If recursion is too deep, then there is a danger of running out of space on the
stack and ultimately program crashes.

iii. It is comparatively difficult to think of the logic of a recursive function.


iv. It also sometimes becomes difficult to debug a recursive code

Consider the example given below:


This program is to print numbers up to 10. We will pass starting number say n
and this function will print Numbers from n to 9

1. def rec_fun(n): # Function Definition


2. if n>=10:
3. return
4. else:
5. print(n)
6. rec_fun(n+1)

7. rec_fun(5) # Function Call

 In above example function is calling itself (line no.6 )


 In line 7, we have called the recursive function rec_fun() with parameter 5.
Let’s understand how this function will print numbers from 5 to 9

line 7. Rec_fun(5) Main Function call

def rec_fun(n):
if n>=10: As value of n is not 10 it will goto else part and print 5
return
Call 1 else:
then copy of the same function will be called again with
print(n) value (5+1)
rec_fun(n+1)

def rec_fun(n):
if n>=10:
Call 2 return Here n is 6 so else part will be executed and it print 6 and
else:
call function again with value (6+1)
rec_fun(n+1)

def rec_fun(n):
if n>=10:
return Here n is 7 so else part will be executed and it print 7 and
Call 3 else: call function again with value (7+1)
print(n)
rec_fun(n+1)

Call 4
def rec_fun(n):
if n>=10:
Call 4 Here n is 8 so else part will be executed and it print 8
else: and call function again with value (8+1)
rec_fun(n+1)

Call 5

def rec_fun(n):
if n>=10: Now n is 9 so else part will be executed and
Call5 return it print 9 and call function again with value
else: (9+1)
print(n)
rec_fun(n+1)

def rec_fun(n): This time n is 10 so if part will be


if n>=10: considered and function control will
return
Call 6 else: be return to the previous function
print(n) call
rec_fun(n+1)
Simple Programs with Recursion

to Print Multiplication Table (2 Methods) CLICK ON


Recursive Function to print Numbers in Reverse Order THE
PROGRAM
Recursive Function to print Factorial of a given Number
LINKS TO
Recursive Function to print Fibonacci Series up to N VIEW
Recursive Function to print fibonacci series up to N Terms

Recursive function to check whether the given no. is Prime

RECURSIVE FUNCTIONS ON ARRAYS/LIST


Calculating Sum of elements of an array through recursive function

Searching an element in an Array using Binary Search Recursive Function

You might also like