SlideShare a Scribd company logo
4
Most read
7
Most read
Recursion
By-Ashish Ranjan
Some computer programming languages
allow a module or function to call itself.
This technique is known as recursion. In
recursion, a function α either calls itself
directly or calls a function β that in turn
calls the original function α.
The function α is called recursive function.
Example − a function calling itself.
int function(int value) {
if(value < 1)
return;
function(value - 1);
printf("%d ",value);
}
Properties
A recursive function can go infinite like a loop. To
avoid infinite running of recursive function, there
are two properties that a recursive function must
have −
Base criteria − There must be at least one base
criteria or condition, such that, when this condition
is met the function stops calling itself recursively.
Progressive approach − The recursive calls should
progress in such a way that each time a recursive
call is made it comes closer to the base criteria.
Implementation
• Many programming languages implement recursion by means
of stacks.
• Generally, whenever a function (caller) calls another function
(callee) or itself as callee, the caller function transfers
execution control to the callee.
• This transfer process may also involve some data to be passed
from the caller to the callee.
• This implies, the caller function has to suspend its execution
temporarily and resume later when the execution control
returns from the callee function.
• Here, the caller function needs to start exactly from the point
of execution where it puts itself on hold. It also needs the
exact same data values it was working on. For this purpose, an
activation record (or stack frame) is created for the caller
function.
This activation record keeps the information about local variables, formal
parameters, return address and all information passed to the caller function.
• Analysis of Recursion
One may argue why to use recursion, as the same task can be done with iteration.
The first reason is, recursion makes a program more readable and because of latest
enhanced CPU systems, recursion is more efficient than iterations.
• Time Complexity
In case of iterations, we take number of iterations to count the time complexity.
Likewise, in case of recursion, assuming everything is constant, we try to figure out
the number of times a recursive call is being made. A call made to a function is Ο(1),
hence the (n) number of times a recursive call is made makes the recursive function
Ο(n).
• Space Complexity
Space complexity is counted as what amount of extra space is required for a module
to execute. In case of iterations, the compiler hardly requires any extra space. The
compiler keeps updating the values of variables used in the iterations. But in case of
recursion, the system needs to store activation record each time a recursive call is
made. Hence, it is considered that space complexity of recursive function may go
higher than that of a function with iteration.

More Related Content

PPT
FUNCTIONS IN c++ PPT
PPTX
Functions in c++
PPTX
classes and objects in C++
PDF
Constructors and Destructors
PPT
Function overloading(c++)
PPTX
Oop c++class(final).ppt
DOCX
Recursion in C++
PDF
Classes and objects
FUNCTIONS IN c++ PPT
Functions in c++
classes and objects in C++
Constructors and Destructors
Function overloading(c++)
Oop c++class(final).ppt
Recursion in C++
Classes and objects

What's hot (20)

PPTX
Classes, objects in JAVA
PPTX
File Handling Python
PPTX
Templates in c++
PPTX
class and objects
PPTX
Virtual base class
PDF
Managing I/O in c++
PPT
Class and object in C++
PPTX
Static Data Members and Member Functions
PPTX
Constructors in C++
PDF
Constructor and Destructor
PPT
Operator Overloading
PPTX
Pointers in c++
PPT
Structure of C++ - R.D.Sivakumar
PDF
Arrays in Java
PPT
Data Structures- Part5 recursion
PPTX
Function C programming
PPTX
Function in c
PPTX
C if else
PPTX
Classes, objects in JAVA
File Handling Python
Templates in c++
class and objects
Virtual base class
Managing I/O in c++
Class and object in C++
Static Data Members and Member Functions
Constructors in C++
Constructor and Destructor
Operator Overloading
Pointers in c++
Structure of C++ - R.D.Sivakumar
Arrays in Java
Data Structures- Part5 recursion
Function C programming
Function in c
C if else
Ad

Similar to Recursion (20)

PPT
Recursion and looping
PDF
Python recursion
PPTX
Recursive Algorithms with their types and implementation
PPTX
10 implementing subprograms
PPTX
3-Recursive Function in programming .pptx
PDF
Data Structure Algorithm - Recursion Book Slides
PDF
Python Programming unit5 (1).pdf
PPT
Runtimeenvironment
PDF
Opject orianted programming in c++ for beg
PPTX
Module_2_1_Building Python Programs_Final.pptx
PPT
Unit 3 principles of programming language
PPTX
Recursion vs. Iteration: Code Efficiency & Structure
DOCX
Data structure notes for introduction, complexity
PDF
Chapter 4: Parallel Programming Languages
PPTX
Activation Racords and Run-time Environments _11_10_2024.pptx
PPTX
Intro To C++ - Class #20: Functions, Recursion
PPT
Reactive programming with examples
PPTX
Recursion Fundamrwersfsdfsdfsfsdfsdfsfsdfsdfsentals.pptx
PPT
OpenMP-Quinn17_L4bOpen <MP_Open MP_Open MP
PDF
Chapter 12 Computer Science ( ICS 12).pdf
Recursion and looping
Python recursion
Recursive Algorithms with their types and implementation
10 implementing subprograms
3-Recursive Function in programming .pptx
Data Structure Algorithm - Recursion Book Slides
Python Programming unit5 (1).pdf
Runtimeenvironment
Opject orianted programming in c++ for beg
Module_2_1_Building Python Programs_Final.pptx
Unit 3 principles of programming language
Recursion vs. Iteration: Code Efficiency & Structure
Data structure notes for introduction, complexity
Chapter 4: Parallel Programming Languages
Activation Racords and Run-time Environments _11_10_2024.pptx
Intro To C++ - Class #20: Functions, Recursion
Reactive programming with examples
Recursion Fundamrwersfsdfsdfsfsdfsdfsfsdfsdfsentals.pptx
OpenMP-Quinn17_L4bOpen <MP_Open MP_Open MP
Chapter 12 Computer Science ( ICS 12).pdf
Ad

Recently uploaded (20)

PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PPTX
Current and future trends in Computer Vision.pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
PPT on Performance Review to get promotions
PDF
Visual Aids for Exploratory Data Analysis.pdf
PPTX
UNIT - 3 Total quality Management .pptx
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
Total quality management ppt for engineering students
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Current and future trends in Computer Vision.pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Information Storage and Retrieval Techniques Unit III
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPT on Performance Review to get promotions
Visual Aids for Exploratory Data Analysis.pdf
UNIT - 3 Total quality Management .pptx
Soil Improvement Techniques Note - Rabbi
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Safety Seminar civil to be ensured for safe working.
Automation-in-Manufacturing-Chapter-Introduction.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Total quality management ppt for engineering students
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf

Recursion

  • 2. Some computer programming languages allow a module or function to call itself. This technique is known as recursion. In recursion, a function α either calls itself directly or calls a function β that in turn calls the original function α. The function α is called recursive function.
  • 3. Example − a function calling itself. int function(int value) { if(value < 1) return; function(value - 1); printf("%d ",value); }
  • 4. Properties A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are two properties that a recursive function must have − Base criteria − There must be at least one base criteria or condition, such that, when this condition is met the function stops calling itself recursively. Progressive approach − The recursive calls should progress in such a way that each time a recursive call is made it comes closer to the base criteria.
  • 5. Implementation • Many programming languages implement recursion by means of stacks. • Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. • This transfer process may also involve some data to be passed from the caller to the callee. • This implies, the caller function has to suspend its execution temporarily and resume later when the execution control returns from the callee function. • Here, the caller function needs to start exactly from the point of execution where it puts itself on hold. It also needs the exact same data values it was working on. For this purpose, an activation record (or stack frame) is created for the caller function.
  • 6. This activation record keeps the information about local variables, formal parameters, return address and all information passed to the caller function.
  • 7. • Analysis of Recursion One may argue why to use recursion, as the same task can be done with iteration. The first reason is, recursion makes a program more readable and because of latest enhanced CPU systems, recursion is more efficient than iterations. • Time Complexity In case of iterations, we take number of iterations to count the time complexity. Likewise, in case of recursion, assuming everything is constant, we try to figure out the number of times a recursive call is being made. A call made to a function is Ο(1), hence the (n) number of times a recursive call is made makes the recursive function Ο(n). • Space Complexity Space complexity is counted as what amount of extra space is required for a module to execute. In case of iterations, the compiler hardly requires any extra space. The compiler keeps updating the values of variables used in the iterations. But in case of recursion, the system needs to store activation record each time a recursive call is made. Hence, it is considered that space complexity of recursive function may go higher than that of a function with iteration.