SlideShare a Scribd company logo
1 of 17
Recursion
Difference between recursion and iteration
based functions
 Recursion uses selection structure.
 Infinite recursion occurs if the recursion step does not reduce the problem in a
manner that converges on some condition (base case) and Infinite recursion can
crash the system.
 Recursion terminates when a base case is recognized.
 Recursion is usually slower than iteration due to the overhead of maintaining the
stack.
 Recursion uses more memory than iteration.
 Recursion makes the code smaller.
continued
 Iteration uses repetition structure.
 An infinite loop occurs with iteration if the loop condition test never becomes false
and Infinite looping uses CPU cycles repeatedly.
 An iteration terminates when the loop condition fails.
 An iteration does not use the stack so it's faster than recursion.
 Iteration consumes less memory.
 Iteration makes the code longer.
Definition
Recursion is one of the most powerful
tool in a proramming language.Recursion
is a way of proramming where function
calls itself again and again.Recursion is
defined as defining something in terms
of itself.
Recursion Essentials
 There must be terminating condition for the problem,
 There must be an if condition in the recursive routine.This if clause specifies the
terminatin condition for the recursion.
 Reversal in te order of execution is the characteristics of every recursive problem.wen a
recursive program is taken for execution , the recursive function calls are not executed
immidiately.They are pushed on the stack as long as terminating condition is
encountered. Asoon as recursive condition is encountered the recursive calls which were
pushed on to stack are removed in reverse order and executed one by one.It means that
last call made would execute first,then the second last call will execute and so on till the
stack is empty.
 Each time a new recursive call is made new memory space will be allocated to each local
variableused by the recursive routine.In oter words during each recursive call to
recursive function the local variables of recursive functions gets a different set of values
but withh the same name.
Contd...
 The duplicated values of a local variables of a recursive calls are pushed on to stack
with its respective calls all there values are available to the respective calls when it
is popped off from the stack.
Disadvantages:it consumes more storage space between the recursive calls along with
local variables are stored on the stack.
The computer may run out of memory if recursive calls are not checked.
It is less efficient in terms of memory and execution speed.
So dividing the recursive problem into two parts:terminating part and recursive part.
Examples
def power(x,n):
if(n==0):
return 1
else:
return(x*power(x,n-1))
a=2
print(power(a,4))
Continued...
def recur_factorial(n):
if (n == 1):
return n
else:
return n*recur_factorial(n-1)
num = 5
# check if the number is negative
if (num < 0):
print("Sorry, factorial does not exist for negative numbers")
elif (num == 0):
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
Continued...
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
# take input from the user
nterms = int(input("How many terms? "))
# check if the number of terms is valid
if (nterms <= 0):
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Continued...
def hcf(a,b):
if b==0:
return a
else:
return hcf(b, a%b)
# this is recursion as hcf() calls itself
# Reading numbers from user
first = int(input('Enter first number: '))
second = int(input('Enter second number: '))
# Function call & displaying output HCF (GCD)
print('HCF or GCD of %d and %d is %d' %(first, second, hcf(first, second)))
Activation record
An activation record usually contains the following information:
1. values for all parameters in the function
2. local variables which can be stored elsewhere
3. Return address to resume control by the caller,the address of the callers
instruction immidiately following the call
4. A dynamic link,which is a link to the callers activation record.
5. The returned value of a function.
Module
Modular programming is a software design technique to split your code into separate parts. These parts are called modules. The focus for
this separation should be to have modules with no or just few dependencies upon other modules. In other words: Minimization of
dependencies is the goal.
If you want to develop programs which are readable, reliable and maintainable without too much effort, you have to use some kind of
modular software design. Especially if your application has a certain size.
A1.py
def greeting(name):
print("Hello, " + name)
import A1
A1.greeting("JAIRAM")
The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):
A1.py
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
import A1
a = A1.person1["age"]
print(a)
Lambda function
A lambda function is a small anonymous function.A lambda function can take any
number of arguments, but can only have one expression.
lambda arguments : expression
x = lambda b : b + 10
print(x(7))
x = lambda a, b : a *b
print(x(4, 6))
x = lambda a, b, c : a + b + c
print(x(3, 3, 2))
Pass statement
 The pass statement is a null statement. But the difference between pass and comment is that
comment is ignored by the interpreter whereas pass is not ignroed.
 The pass statement is generally used as a placeholder i.e. when the user does not know what code
to write.
 user simply places pass at that line. Sometimes, pass is used when the user doesn’t want any code
to execute.
 user simply places pass there as empty code is not allowed in loops, function definitions, class
definitions, or in if statements.
 using pass statement user avoids this error.
l =[‘u', ‘g', ‘j', ‘d']
for i in l:
if(i ==‘u'):
pass
else:
print(i)

More Related Content

Similar to Recursion vs. Iteration: Code Efficiency & Structure

Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxpriestmanmable
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfdata2businessinsight
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)SURBHI SAROHA
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptxajajkhan16
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Ziyauddin Shaik
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptRajasekhar364622
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2Prerna Sharma
 
C basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kellaC basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kellaManoj Kumar kothagulla
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptxLizhen Shi
 

Similar to Recursion vs. Iteration: Code Efficiency & Structure (20)

Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Lecture 08.pptx
Lecture 08.pptxLecture 08.pptx
Lecture 08.pptx
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
C basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kellaC basic questions&amp;ansrs by shiva kumar kella
C basic questions&amp;ansrs by shiva kumar kella
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
 
Recursion CBSE Class 12
Recursion CBSE Class 12Recursion CBSE Class 12
Recursion CBSE Class 12
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 

Recently uploaded

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 

Recursion vs. Iteration: Code Efficiency & Structure

  • 2. Difference between recursion and iteration based functions  Recursion uses selection structure.  Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition (base case) and Infinite recursion can crash the system.  Recursion terminates when a base case is recognized.  Recursion is usually slower than iteration due to the overhead of maintaining the stack.  Recursion uses more memory than iteration.  Recursion makes the code smaller.
  • 3. continued  Iteration uses repetition structure.  An infinite loop occurs with iteration if the loop condition test never becomes false and Infinite looping uses CPU cycles repeatedly.  An iteration terminates when the loop condition fails.  An iteration does not use the stack so it's faster than recursion.  Iteration consumes less memory.  Iteration makes the code longer.
  • 4. Definition Recursion is one of the most powerful tool in a proramming language.Recursion is a way of proramming where function calls itself again and again.Recursion is defined as defining something in terms of itself.
  • 5. Recursion Essentials  There must be terminating condition for the problem,  There must be an if condition in the recursive routine.This if clause specifies the terminatin condition for the recursion.  Reversal in te order of execution is the characteristics of every recursive problem.wen a recursive program is taken for execution , the recursive function calls are not executed immidiately.They are pushed on the stack as long as terminating condition is encountered. Asoon as recursive condition is encountered the recursive calls which were pushed on to stack are removed in reverse order and executed one by one.It means that last call made would execute first,then the second last call will execute and so on till the stack is empty.  Each time a new recursive call is made new memory space will be allocated to each local variableused by the recursive routine.In oter words during each recursive call to recursive function the local variables of recursive functions gets a different set of values but withh the same name.
  • 6. Contd...  The duplicated values of a local variables of a recursive calls are pushed on to stack with its respective calls all there values are available to the respective calls when it is popped off from the stack. Disadvantages:it consumes more storage space between the recursive calls along with local variables are stored on the stack. The computer may run out of memory if recursive calls are not checked. It is less efficient in terms of memory and execution speed. So dividing the recursive problem into two parts:terminating part and recursive part.
  • 8.
  • 9.
  • 10.
  • 11. Continued... def recur_factorial(n): if (n == 1): return n else: return n*recur_factorial(n-1) num = 5 # check if the number is negative if (num < 0): print("Sorry, factorial does not exist for negative numbers") elif (num == 0): print("The factorial of 0 is 1") else: print("The factorial of", num, "is", recur_factorial(num))
  • 12. Continued... def recur_fibo(n): if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # take input from the user nterms = int(input("How many terms? ")) # check if the number of terms is valid if (nterms <= 0): print("Plese enter a positive integer") else: print("Fibonacci sequence:") for i in range(nterms): print(recur_fibo(i))
  • 13. Continued... def hcf(a,b): if b==0: return a else: return hcf(b, a%b) # this is recursion as hcf() calls itself # Reading numbers from user first = int(input('Enter first number: ')) second = int(input('Enter second number: ')) # Function call & displaying output HCF (GCD) print('HCF or GCD of %d and %d is %d' %(first, second, hcf(first, second)))
  • 14. Activation record An activation record usually contains the following information: 1. values for all parameters in the function 2. local variables which can be stored elsewhere 3. Return address to resume control by the caller,the address of the callers instruction immidiately following the call 4. A dynamic link,which is a link to the callers activation record. 5. The returned value of a function.
  • 15. Module Modular programming is a software design technique to split your code into separate parts. These parts are called modules. The focus for this separation should be to have modules with no or just few dependencies upon other modules. In other words: Minimization of dependencies is the goal. If you want to develop programs which are readable, reliable and maintainable without too much effort, you have to use some kind of modular software design. Especially if your application has a certain size. A1.py def greeting(name): print("Hello, " + name) import A1 A1.greeting("JAIRAM") The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc): A1.py person1 = { "name": "John", "age": 36, "country": "Norway" } import A1 a = A1.person1["age"] print(a)
  • 16. Lambda function A lambda function is a small anonymous function.A lambda function can take any number of arguments, but can only have one expression. lambda arguments : expression x = lambda b : b + 10 print(x(7)) x = lambda a, b : a *b print(x(4, 6)) x = lambda a, b, c : a + b + c print(x(3, 3, 2))
  • 17. Pass statement  The pass statement is a null statement. But the difference between pass and comment is that comment is ignored by the interpreter whereas pass is not ignroed.  The pass statement is generally used as a placeholder i.e. when the user does not know what code to write.  user simply places pass at that line. Sometimes, pass is used when the user doesn’t want any code to execute.  user simply places pass there as empty code is not allowed in loops, function definitions, class definitions, or in if statements.  using pass statement user avoids this error. l =[‘u', ‘g', ‘j', ‘d'] for i in l: if(i ==‘u'): pass else: print(i)