SlideShare a Scribd company logo
Recursion
CSE102 – Programming
Fundamentals
Recursion Introduction
Most mathematical functions are described by a
simple formula
 C = 5 × (F – 32) / 9
 It is trivial to program the formula
Mathematical functions are sometimes defined in a
less standard form
 ƒ(0) = 0 and ƒ(x) = 2ƒ(x-1) + x2
 Then, ƒ(0) = 0, ƒ(1) = 1, ƒ(2) = 6, ƒ(3) = 21, …
A function that is defined in terms of itself is
recursive, and C allows recursive functions
Example
Factorial: N! = N * (N-1)!
Fibonacci Number: F(n) = F(n-1)+F(n-2)
xn
= x × xn–1
Tower of Hanoi
Example: factorial
Example: factorials
 5! = 5 * 4 * 3 * 2 * 1
 Notice that
 5! = 5 * 4!
 4! = 4 * 3! ...
 Can compute factorials recursively using factorials
 Solve base case (1! = 0! = 1) then plug in
 2! = 2 * 1! = 2 * 1 = 2;
 3! = 3 * 2! = 3 * 2 = 6;
Factorial
 The following function computes n! recursively,
using the formula n! = n × (n – 1)!:
int factorial(int n)
{
int t;
if (n <= 1)
t=1;
else
t=n * factorial(n-1);
return t;
}
5.13 Recursion
5!
(a) Sequence of recursive calls. (b) Values returned from each recursive call.
Final value = 120
5! = 5 * 24 = 120 is returned
4! = 4 * 6 = 24 is returned
2! = 2 * 1 = 2 is returned
3! = 3 * 2 = 6 is returned
1 returned
5 * 4!
1
4 * 3!
3 * 2!
2 * 1!
5!
5 * 4!
1
4 * 3!
3 * 2!
2 * 1!
Factorial()
int factorial (int n) { | factorial(4) =
int t; | 4 * factorial(3) =
if (n <= 1) t = 1; /* BASE */ | 4 * 3 * factorial(2) =
else t= n * factorial(n-1); | 4 * 3 * 2 * factorial(1) =
return t; /* Make Progress */ | 4 * 3 * 2 * 1 = 24
}
Fundamental Rules of Recursion
Base Case: Must have some base cases which can
be solved without recursion
Making Progress: for the case that are to be solved
recursively, the recursive call always make progress
toward the base case
If you do not follow the above rule, your program
would not terminate
An Example of Non-Terminating Recursion
int BAD (int x) {
if (x==0) return (0);
else return( BAD( x/3 + 1 ) + x–1 );
}
main() { int y; y = BAD(1) + BAD(4);}
Recursion
 The following recursive function computes xn
, using
the formula xn
= x × xn–1
.
int power(int x, int n)
{
int res;
if (n == 0)
return 1;
else{
res=x * power(x, n - 1);
return res;
}
}
Recursion
 Both fact and power are careful to test a
“termination condition” as soon as they’re called.
 All recursive functions need some kind of
termination condition in order to prevent infinite
recursion.
Recursive Functions: f(x)=2*f(x-1)+x2
A function that calls itself either directly of indirectly
int f (int x)
{
if( x == 0 ) return (0); /* Base Case */
else return( 2*f(x-1) + x*x ); /* Make Progress*/
}
5.14 Recursion Example: Fibonacci Series
Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
 Each number is the sum of the previous two
 Can be solved recursively:
 fib( n ) = fib( n - 1 ) + fib( n – 2 )
 Code for the fibonacci function
long fibonacci( long n )
{
if (n == 0 || n == 1) // base case
return n;
else
return fibonacci( n - 1) + // recursive step
fibonacci( n – 2 );
}
 
5.14 Example Using Recursion: The Fibonacci Series
Set of recursive calls to function fibonacci
f( 3 )
f( 1 )f( 2 )
f( 1 ) f( 0 ) return 1
return 1 return 0
return +
+return
 
5.15 Recursion vs. Iteration
Repetition
 Iteration: explicit loop
 Recursion: repeated function calls
Termination
 Iteration: loop condition fails
 Recursion: base case recognized
Both can have infinite loops
Balance between performance and software engineering
 Iteration achieves better performance (e.g., numerical
computation)
 Why? Function call involves lots of overhead
 Recursion allows simple & efficient programming for complex
problems, especially in non-numerical applications
 e.g., quick sort, depth first traversal, tower of hanoi, ….

More Related Content

PPT
code optimization 1...code optimization-1221849738688969-9
PPTX
Compiler Optimization Presentation
PPTX
Recursion for GCE AS Computing
PPTX
Introduction to code optimization by dipankar
PDF
Introduction to Recursion (Python)
PPTX
Lectue five
PDF
Network security CS6
PPTX
WBF.pptx
code optimization 1...code optimization-1221849738688969-9
Compiler Optimization Presentation
Recursion for GCE AS Computing
Introduction to code optimization by dipankar
Introduction to Recursion (Python)
Lectue five
Network security CS6
WBF.pptx

What's hot (20)

PDF
Network Security CS3-4
PPT
Tsp branch and bound
PDF
Doc 20180130-wa0006
PPTX
Polynomial functions
PDF
6-Python-Recursion.pdf
PDF
01 sets, relations and functions
PPT
Building blocks 2
PDF
5.1part2 foil
PPSX
Introduction to Function, Domain and Range - Mohd Noor
PDF
Compiler lec 5
KEY
Function Basics Math Wiki
PPTX
Dynamic Programming - Part 1
PPT
PPt on Functions
PPT
Storyboard math
PPTX
Section 1.1
PPTX
Functions
PDF
Moody chart, M-File for finding Friction factor using equation
PPT
Objective 1 - Identifying Functions
PPTX
Generating code from dags
Network Security CS3-4
Tsp branch and bound
Doc 20180130-wa0006
Polynomial functions
6-Python-Recursion.pdf
01 sets, relations and functions
Building blocks 2
5.1part2 foil
Introduction to Function, Domain and Range - Mohd Noor
Compiler lec 5
Function Basics Math Wiki
Dynamic Programming - Part 1
PPt on Functions
Storyboard math
Section 1.1
Functions
Moody chart, M-File for finding Friction factor using equation
Objective 1 - Identifying Functions
Generating code from dags
Ad

Viewers also liked (20)

PDF
帛琉五日
DOC
CVJM Update NYAMBE-F (2)
PDF
Bijoux ronaldo
PDF
環球影城六日
PPT
Power point in ped5 herwin
PDF
Vacante en Director de Ventas - Esri Colombia
PPTX
Cyber security
PPT
Slide Da Atividade Educacional
PDF
20160304埃及10日 直客版
PDF
20131006張家界八日
PPT
Binary search trees (1)
PDF
Palestra Mariana
DOCX
ConnieJusticeCV-2016
PDF
Manual conciliação ok
PPTX
Linea de tiempo de literatura por monica llumiquinga
PDF
Material de trabajo informática i
PDF
Innovative Methods and Technologies in Project Management: Project Management...
PDF
Innovative Methods and Technologies in Project Management: Project Management...
PDF
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
帛琉五日
CVJM Update NYAMBE-F (2)
Bijoux ronaldo
環球影城六日
Power point in ped5 herwin
Vacante en Director de Ventas - Esri Colombia
Cyber security
Slide Da Atividade Educacional
20160304埃及10日 直客版
20131006張家界八日
Binary search trees (1)
Palestra Mariana
ConnieJusticeCV-2016
Manual conciliação ok
Linea de tiempo de literatura por monica llumiquinga
Material de trabajo informática i
Innovative Methods and Technologies in Project Management: Project Management...
Innovative Methods and Technologies in Project Management: Project Management...
Chancen 2013 - Seminare und Trainings - TUI Consulting und Services.pdf
Ad

Similar to 14 recursion (20)

PDF
Recursion.pdf
PPT
Lec-6 Recursion of Data Structures & Algorithms
PPT
Data Structures- Part5 recursion
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
PPTX
Recursion and Sorting Algorithms
PPTX
06 Recursion in C.pptx
PDF
Iterations and Recursions
PPT
Lecture9 recursion
PPTX
Recursion
PDF
062636636366363773737373733+73737733+7.pdf
PPT
lecture 10 Recursive Function and Macros.ppt
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
PPTX
Recursion in Data Structure
PDF
Recursion examples
PPTX
Recursion
PDF
Lecture 2.4 Recursion.pdf
PDF
recursion2
PPTX
Unit-I Recursion.pptx
PPTX
Recursion part 1
Recursion.pdf
Lec-6 Recursion of Data Structures & Algorithms
Data Structures- Part5 recursion
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
Recursion and Sorting Algorithms
06 Recursion in C.pptx
Iterations and Recursions
Lecture9 recursion
Recursion
062636636366363773737373733+73737733+7.pdf
lecture 10 Recursive Function and Macros.ppt
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
Recursion in Data Structure
Recursion examples
Recursion
Lecture 2.4 Recursion.pdf
recursion2
Unit-I Recursion.pptx
Recursion part 1

More from Himadri Sen Gupta (8)

DOCX
Report of industrial training
PPT
17 linkedlist (1)
PPT
Linked lists
PPTX
Report of industrial training
17 linkedlist (1)
Linked lists

Recently uploaded (20)

PPTX
Internet of Things (IOT) - A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Construction Project Organization Group 2.pptx
PPTX
web development for engineering and engineering
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Well-logging-methods_new................
PPT
Mechanical Engineering MATERIALS Selection
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
composite construction of structures.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Internet of Things (IOT) - A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
UNIT 4 Total Quality Management .pptx
OOP with Java - Java Introduction (Basics)
Embodied AI: Ushering in the Next Era of Intelligent Systems
Construction Project Organization Group 2.pptx
web development for engineering and engineering
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Well-logging-methods_new................
Mechanical Engineering MATERIALS Selection
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
bas. eng. economics group 4 presentation 1.pptx
additive manufacturing of ss316l using mig welding
composite construction of structures.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

14 recursion

  • 2. Recursion Introduction Most mathematical functions are described by a simple formula  C = 5 × (F – 32) / 9  It is trivial to program the formula Mathematical functions are sometimes defined in a less standard form  ƒ(0) = 0 and ƒ(x) = 2ƒ(x-1) + x2  Then, ƒ(0) = 0, ƒ(1) = 1, ƒ(2) = 6, ƒ(3) = 21, … A function that is defined in terms of itself is recursive, and C allows recursive functions
  • 3. Example Factorial: N! = N * (N-1)! Fibonacci Number: F(n) = F(n-1)+F(n-2) xn = x × xn–1 Tower of Hanoi
  • 4. Example: factorial Example: factorials  5! = 5 * 4 * 3 * 2 * 1  Notice that  5! = 5 * 4!  4! = 4 * 3! ...  Can compute factorials recursively using factorials  Solve base case (1! = 0! = 1) then plug in  2! = 2 * 1! = 2 * 1 = 2;  3! = 3 * 2! = 3 * 2 = 6;
  • 5. Factorial  The following function computes n! recursively, using the formula n! = n × (n – 1)!: int factorial(int n) { int t; if (n <= 1) t=1; else t=n * factorial(n-1); return t; }
  • 6. 5.13 Recursion 5! (a) Sequence of recursive calls. (b) Values returned from each recursive call. Final value = 120 5! = 5 * 24 = 120 is returned 4! = 4 * 6 = 24 is returned 2! = 2 * 1 = 2 is returned 3! = 3 * 2 = 6 is returned 1 returned 5 * 4! 1 4 * 3! 3 * 2! 2 * 1! 5! 5 * 4! 1 4 * 3! 3 * 2! 2 * 1!
  • 7. Factorial() int factorial (int n) { | factorial(4) = int t; | 4 * factorial(3) = if (n <= 1) t = 1; /* BASE */ | 4 * 3 * factorial(2) = else t= n * factorial(n-1); | 4 * 3 * 2 * factorial(1) = return t; /* Make Progress */ | 4 * 3 * 2 * 1 = 24 }
  • 8. Fundamental Rules of Recursion Base Case: Must have some base cases which can be solved without recursion Making Progress: for the case that are to be solved recursively, the recursive call always make progress toward the base case If you do not follow the above rule, your program would not terminate An Example of Non-Terminating Recursion int BAD (int x) { if (x==0) return (0); else return( BAD( x/3 + 1 ) + x–1 ); } main() { int y; y = BAD(1) + BAD(4);}
  • 9. Recursion  The following recursive function computes xn , using the formula xn = x × xn–1 . int power(int x, int n) { int res; if (n == 0) return 1; else{ res=x * power(x, n - 1); return res; } }
  • 10. Recursion  Both fact and power are careful to test a “termination condition” as soon as they’re called.  All recursive functions need some kind of termination condition in order to prevent infinite recursion.
  • 11. Recursive Functions: f(x)=2*f(x-1)+x2 A function that calls itself either directly of indirectly int f (int x) { if( x == 0 ) return (0); /* Base Case */ else return( 2*f(x-1) + x*x ); /* Make Progress*/ }
  • 12. 5.14 Recursion Example: Fibonacci Series Fibonacci series: 0, 1, 1, 2, 3, 5, 8...  Each number is the sum of the previous two  Can be solved recursively:  fib( n ) = fib( n - 1 ) + fib( n – 2 )  Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci( n - 1) + // recursive step fibonacci( n – 2 ); }  
  • 13. 5.14 Example Using Recursion: The Fibonacci Series Set of recursive calls to function fibonacci f( 3 ) f( 1 )f( 2 ) f( 1 ) f( 0 ) return 1 return 1 return 0 return + +return  
  • 14. 5.15 Recursion vs. Iteration Repetition  Iteration: explicit loop  Recursion: repeated function calls Termination  Iteration: loop condition fails  Recursion: base case recognized Both can have infinite loops Balance between performance and software engineering  Iteration achieves better performance (e.g., numerical computation)  Why? Function call involves lots of overhead  Recursion allows simple & efficient programming for complex problems, especially in non-numerical applications  e.g., quick sort, depth first traversal, tower of hanoi, ….