XII CS - Chapter - 2 - Recursion-Notes
XII CS - Chapter - 2 - Recursion-Notes
ii. If recursion is too deep, then there is a danger of running out of space on the
stack and ultimately program crashes.
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)