SlideShare a Scribd company logo
6
Most read
8
Most read
10
Most read
TAIL RECURSION
SOURAV BHUNIA
ID:1811100001057
SISTER NIVEDITA UNIVERSITY
WHAT IS RECURSION??
• The process in which a function calls itself directly or
indirectly is called recursion and the corresponding
function is called as recursive function.
RECURSION TYPE:
1.Tail Recursion
2.Head Recursion
3.Tree Recursion
4.Indirect Recursion
5.Nested Recursion
RECURSIONTYPE
Recursion
TAIL
RECURSION
HEAD
RECURSION
Tree
Recursion
Indirect
Recursion
Nested
Recursion
TAIL RECURSION:
If a recursive function is calling itself and that recursive call is the
last statement of the recursive function, then it is called as tail
recursion.
After the calling of the last statement there is nothing to execute in
the recursive function.
EXAMPLE OF TAIL RECURSION:
Void fun(int n)
{
if(n>0)
{
Printf(“%d”,n);
fun(n-1);
}
}
>>>fun(3)
Result produced: 3,2,1
Last Statement of the recursive function
TAIL RECURSION OR NOT..
• Void fun(int n)
• {
if(n>0)
{
//Some code
fun(n-1)+n;
}
• }
All these operation perform in calling time only. The function not
be performing any operation in returning time
unless result of the function is known addition
operation is not performed.
In this case function has to perform operation on
returning time , so this is not a tail recursion.
It is also the last statement of the recursive function
TAIL RECURSION VS. LOOP:
Void fun(int n)
{
while(n>0)
{
Printf(“%d”,n);
n--;
}
}
Passing value:fun(3)
Every recursive function can be written
as loop and vise versa.
COMPARISON :
● Time complexity: O(n) O(n)
● Space complexity: O(1) O(n)
LOOP Tail recursion
Tail Recursive VS. Non-Tail Recursive
1.The tail recursive functions considered better than non tail recursive
functions as tail-recursion can be optimized by compiler.
2.The idea used by compilers to optimize tail-recursive functions is
simple, since the recursive call is the last statement, there is nothing
left to do in the current function, so saving the current function’s stack
frame is of no use
CONCLUSION:
● It is better to convert tail recursion into loop for better
efficiency in terms of space.
● This is not true for all recursion or loop.
● It is efficient only in tail recursion.
THANK YOU!

More Related Content

PPTX
unit-4-dynamic programming
PPTX
Hill climbing algorithm
PPT
Dinive conquer algorithm
PPTX
NFA Converted to DFA , Minimization of DFA , Transition Diagram
PPT
Algorithm analysis
PPT
5.1 greedy
PPT
Chap4
PPTX
Operating system Dead lock
unit-4-dynamic programming
Hill climbing algorithm
Dinive conquer algorithm
NFA Converted to DFA , Minimization of DFA , Transition Diagram
Algorithm analysis
5.1 greedy
Chap4
Operating system Dead lock

What's hot (20)

PPT
Divide and conquer
PPTX
AI-03 Problems State Space.pptx
PPTX
Alpha beta
PPTX
NP completeness
PPTX
Paging using tlb in operating System.pptx
PPT
Propositional Logic and Pridicate logic
PDF
Binary Search - Design & Analysis of Algorithms
PDF
Hill climbing algorithm in artificial intelligence
PPTX
Propositional logic
PPTX
Divide and conquer - Quick sort
PPT
Asymptotic notation
PDF
FP 301 OOP FINAL PAPER
PPTX
Distributed Query Processing
PPTX
Asymptotic Notations
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PDF
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdf
PPTX
Asymptotic Notation
PPTX
AI - Local Search - Hill Climbing
PDF
I. Hill climbing algorithm II. Steepest hill climbing algorithm
PDF
State Space Search in ai
Divide and conquer
AI-03 Problems State Space.pptx
Alpha beta
NP completeness
Paging using tlb in operating System.pptx
Propositional Logic and Pridicate logic
Binary Search - Design & Analysis of Algorithms
Hill climbing algorithm in artificial intelligence
Propositional logic
Divide and conquer - Quick sort
Asymptotic notation
FP 301 OOP FINAL PAPER
Distributed Query Processing
Asymptotic Notations
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
UNIT - I PROBLEM SOLVING AGENTS and EXAMPLES.pptx.pdf
Asymptotic Notation
AI - Local Search - Hill Climbing
I. Hill climbing algorithm II. Steepest hill climbing algorithm
State Space Search in ai
Ad

Similar to Tail recursion (20)

PPTX
Complexity Analysis of Recursive Function
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PPTX
Lecture 03 algorithm analysis
PPTX
Recursive Algorithms with their types and implementation
PPT
17recursion
PPT
Data Structures- Part5 recursion
PDF
14. Recursion.pdf
PPTX
Recursion vs. Iteration: Code Efficiency & Structure
PPTX
Unit-I Recursion.pptx
PPT
Introduction To Functional Programming
PPT
Recursion and looping
PPT
recursion.ppt
PPT
Scala functions
PDF
Chapter 7 recursion handouts with notes
DOCX
Recursion in C++
PPTX
Recursion(Advanced data structure)
PPTX
Tail recursion
PPTX
Tail Recursion in data structure
PPTX
(Recursion)ads
PDF
Python Programming unit5 (1).pdf
Complexity Analysis of Recursive Function
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Lecture 03 algorithm analysis
Recursive Algorithms with their types and implementation
17recursion
Data Structures- Part5 recursion
14. Recursion.pdf
Recursion vs. Iteration: Code Efficiency & Structure
Unit-I Recursion.pptx
Introduction To Functional Programming
Recursion and looping
recursion.ppt
Scala functions
Chapter 7 recursion handouts with notes
Recursion in C++
Recursion(Advanced data structure)
Tail recursion
Tail Recursion in data structure
(Recursion)ads
Python Programming unit5 (1).pdf
Ad

Recently uploaded (20)

PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
additive manufacturing of ss316l using mig welding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPT
Project quality management in manufacturing
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Digital Logic Computer Design lecture notes
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Sustainable Sites - Green Building Construction
additive manufacturing of ss316l using mig welding
R24 SURVEYING LAB MANUAL for civil enggi
OOP with Java - Java Introduction (Basics)
Foundation to blockchain - A guide to Blockchain Tech
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Project quality management in manufacturing
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mechanical Engineering MATERIALS Selection
bas. eng. economics group 4 presentation 1.pptx
Geodesy 1.pptx...............................................
Well-logging-methods_new................
573137875-Attendance-Management-System-original
Lecture Notes Electrical Wiring System Components
Digital Logic Computer Design lecture notes

Tail recursion

  • 2. WHAT IS RECURSION?? • The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function.
  • 3. RECURSION TYPE: 1.Tail Recursion 2.Head Recursion 3.Tree Recursion 4.Indirect Recursion 5.Nested Recursion
  • 5. TAIL RECURSION: If a recursive function is calling itself and that recursive call is the last statement of the recursive function, then it is called as tail recursion. After the calling of the last statement there is nothing to execute in the recursive function.
  • 6. EXAMPLE OF TAIL RECURSION: Void fun(int n) { if(n>0) { Printf(“%d”,n); fun(n-1); } } >>>fun(3) Result produced: 3,2,1 Last Statement of the recursive function
  • 7. TAIL RECURSION OR NOT.. • Void fun(int n) • { if(n>0) { //Some code fun(n-1)+n; } • } All these operation perform in calling time only. The function not be performing any operation in returning time unless result of the function is known addition operation is not performed. In this case function has to perform operation on returning time , so this is not a tail recursion. It is also the last statement of the recursive function
  • 8. TAIL RECURSION VS. LOOP: Void fun(int n) { while(n>0) { Printf(“%d”,n); n--; } } Passing value:fun(3) Every recursive function can be written as loop and vise versa.
  • 9. COMPARISON : ● Time complexity: O(n) O(n) ● Space complexity: O(1) O(n) LOOP Tail recursion
  • 10. Tail Recursive VS. Non-Tail Recursive 1.The tail recursive functions considered better than non tail recursive functions as tail-recursion can be optimized by compiler. 2.The idea used by compilers to optimize tail-recursive functions is simple, since the recursive call is the last statement, there is nothing left to do in the current function, so saving the current function’s stack frame is of no use
  • 11. CONCLUSION: ● It is better to convert tail recursion into loop for better efficiency in terms of space. ● This is not true for all recursion or loop. ● It is efficient only in tail recursion.