function Flashcards
return a,b
is it correct or is it gives error
correct
it returns multiple value i.e. a and b
when recursive call is made then ____________ will be created in _________________
when recursive call is made then activation record will be created in stack
Syntax to create user defined functions
def function_name(parameters):
statement1
statement2
:
return value
give example of tail recursion
def tail_rec(n):
if n==0:
return
else:
print(n)
return tail_rec(n-1)
tail_rec(4)
tail recursion
In the program the very last statement is the only one recursive call and there is no other statement after that then it is called as tail recursion
non-tail recursion
in the program after the recursive call some statements to execute, then it is called non-tail recursion
recursion
function calling itself is called recursion
tell the error
def summation(a,b):
summation = a+b
return a,b
no error
Function calling itself is called __________
recursion
is ‘def’ required to create function
yes, required
def wish():
print(‘Good evening’)
print(‘hey we are in function’)
for i in range(3):
wish()
Good evening
hey we are in function
Good evening
hey we are in function
Good evening
hey we are in function
if recursion doesn’t have termination condition, it will leads to _______________________
stack overflow
types of functions
1.)Built-in functions
2.)User defined functions
Recursion function should have _____________ condition
termination or base
how can we write equivalent non recursive program of tail recursion
by using loops
Main advantage of functions
Code reusability
example of non tail recursion
def nt(n):
if n<=1:
return
else:
nt(n-2)
print(n)
nt(n-1)
print(n-2)
is ‘return’ required to create function
No
return is optional
in the program after the recursive call some statements to execute, then it is called
non-tail recursion
can we write equivalent non recursive program of tail recursion
yes by using loops
types of recursion
4 types
1.)tail recursion
2.)non-tail recursion
3.)indirect recursion
4.)nested recursion
recursion internally uses _______________ data structure
stack
can we write equivalent non recursive program of non tail recursion
yes but it is very difficult
stack overflow
if recursion doesn’t have termination condition, it will leads to stack overflow
another name of ‘termination condition’
base condition
Activation record
the stack frame(memory) allocated to the recursive function call as activation record.
The creation and deletion of activation record depends on actual function calling sequence
special thing about ‘return’ of python which separates it from other languages
in python: a function can return any number of values
in other languages: a function return almost one value
keywords uses when we create function
def
return
Indirect recursion
The two or more functions calling each other is called indirect recursion
Nested Recursion
A recursion function which is passing itself as parameter to a recursive call is called as nested recursion
__name__
It is special variable in python which will be added internally. This variable stores information regarding whether the program is executed as individual program or as a module
explain the value of __name__
if the __name__ = __main__ then the program is executed as individual
if the __name != __main__ then the program is executed as module and __name__ will return the name of module where it is defined