SlideShare a Scribd company logo
Name-Pravesh Sakhare
Roll No-49
Subject-Web Technology
Branch-CsE(Cyber sEcUriTy)
Topic-Javascript Recursion
And here your subtitle.
Recursive Functions • Recursive functions are functions that call themselves. •
Data structures, especially linked implementations of binary trees, sometimes
use recursive functions
Example: Factorial Function • The factorial function is often written as a
recursive function. • The factorial of a positive integer is the product of all
positive integers less than or equal to the number. 5 factorial is written 5! 5! =
5 * 4 * 3 * 2 * 1 = 120 3! = 3 * 2 * 1 = 6 0! is defined to be
Recursive Process (cont.)
x = factorial( 4 );
int factorial( int num )
{ if ( num == 0 || num == 1 ) 4 replaces each
occurrence of num Recursive Process
return 1;
return num * factorial( num – 1 );
}
Base Case
1 int factorial( int num )
2 {
3 if ( num == 0 || num == 1 )
4 return 1;
5 return num * factorial( num – 1 );
6 }
Notice that these lines stopped the recursion – without these
lines, the function will call itself over and over again (infinite
recursion
Infinite Recursion
1 int factorial( int num )
2 {
3 if ( num == 0 || num == 1 )
4 return 1;
5 return num * factorial( num – 1 );
6 }
If one makes a mistake and inputs a negative number into
this function: factorial( -2 ); what will happen? Infinite
recursion
Guidelines
• There must be a base case that stops recursion.
• Each recursive call should approach the base case.
• The recursive function call should work for the base case.
• The recursive function call should work for the case next to the base
case.
• The recursive function should make logical sense, assuming that the
recursive function call inside it does everything it should do
Recursion on a Linked List (cont.)
bool search( Node *ptr, T& foundItem, T& target)
{
.if ( ptr == NULL ) return false;
if ( ptr->info == target )
{
foundItem = ptr->info; return true;
}
return search( ptr->next, foundItem, target );
}
Searching for a Mercedes in the linked list: Car mercedes, foundCar;
… bool found = search( start, foundCar, mercedes );
Thank You

More Related Content

PPTX
Recursion
PPT
Data Structures- Part5 recursion
PDF
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
PPT
Lec-6 Recursion of Data Structures & Algorithms
PPT
Lecture9 recursion
PPTX
Recursion and Sorting Algorithms
PDF
Recursion.pdf
PPT
Recursion.ppt
Recursion
Data Structures- Part5 recursion
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
Lec-6 Recursion of Data Structures & Algorithms
Lecture9 recursion
Recursion and Sorting Algorithms
Recursion.pdf
Recursion.ppt

Similar to WT-Pravesh Sakhare.pptx (20)

PPT
14 recursion
PPTX
Lecture09 recursion
PPTX
lecture4-recursion.pptx
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
PPTX
Recursion in Data Structure
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PDF
recursion2
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
PPTX
3-Recursive Function in programming .pptx
PPTX
Introduction to Recursion
PPT
FUNDAMETAL ALG.ppt
PPTX
Recursion
DOCX
Recursion in C++
PDF
Programming in Java: Recursion
PPTX
(Recursion)ads
PPTX
Recursion(Advanced data structure)
PPTX
Recursion is used in programming languages to use a procedure multiple times ...
PPTX
Recursive Algorithm Detailed Explanation
PDF
Iterations and Recursions
PPT
Ch10 Recursion
14 recursion
Lecture09 recursion
lecture4-recursion.pptx
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
Recursion in Data Structure
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
recursion2
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
3-Recursive Function in programming .pptx
Introduction to Recursion
FUNDAMETAL ALG.ppt
Recursion
Recursion in C++
Programming in Java: Recursion
(Recursion)ads
Recursion(Advanced data structure)
Recursion is used in programming languages to use a procedure multiple times ...
Recursive Algorithm Detailed Explanation
Iterations and Recursions
Ch10 Recursion

Recently uploaded (20)

PDF
Company Presentation pada Perusahaan ADB.pdf
PPTX
Azure Data management Engineer project.pptx
PPTX
Trading Procedures (1).pptxcffcdddxxddsss
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
Presentation1.pptxvhhh. H ycycyyccycycvvv
PPTX
batch data Retailer Data management Project.pptx
PDF
Digital Infrastructure – Powering the Connected Age
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
PDF
Taxes Foundatisdcsdcsdon Certificate.pdf
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Lecture 1 Intro in Inferential Statistics.pptx
PPTX
Purple and Violet Modern Marketing Presentation (1).pptx
PPTX
Extract Transformation Load (3) (1).pptx
PDF
Report The-State-of-AIOps 20232032 3.pdf
PDF
AI Lect 2 Identifying AI systems, branches of AI, etc.pdf
PPTX
Machine Learning Solution for Power Grid Cybersecurity with GraphWavelets
PDF
Research about a FoodFolio app for personalized dietary tracking and health o...
PDF
Master Databricks SQL with AccentFuture – The Future of Data Warehousing
PDF
Company Profile 2023 PT. ZEKON INDONESIA.pdf
Company Presentation pada Perusahaan ADB.pdf
Azure Data management Engineer project.pptx
Trading Procedures (1).pptxcffcdddxxddsss
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
Presentation1.pptxvhhh. H ycycyyccycycvvv
batch data Retailer Data management Project.pptx
Digital Infrastructure – Powering the Connected Age
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
Taxes Foundatisdcsdcsdon Certificate.pdf
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Lecture 1 Intro in Inferential Statistics.pptx
Purple and Violet Modern Marketing Presentation (1).pptx
Extract Transformation Load (3) (1).pptx
Report The-State-of-AIOps 20232032 3.pdf
AI Lect 2 Identifying AI systems, branches of AI, etc.pdf
Machine Learning Solution for Power Grid Cybersecurity with GraphWavelets
Research about a FoodFolio app for personalized dietary tracking and health o...
Master Databricks SQL with AccentFuture – The Future of Data Warehousing
Company Profile 2023 PT. ZEKON INDONESIA.pdf

WT-Pravesh Sakhare.pptx

  • 1. Name-Pravesh Sakhare Roll No-49 Subject-Web Technology Branch-CsE(Cyber sEcUriTy) Topic-Javascript Recursion And here your subtitle.
  • 2. Recursive Functions • Recursive functions are functions that call themselves. • Data structures, especially linked implementations of binary trees, sometimes use recursive functions Example: Factorial Function • The factorial function is often written as a recursive function. • The factorial of a positive integer is the product of all positive integers less than or equal to the number. 5 factorial is written 5! 5! = 5 * 4 * 3 * 2 * 1 = 120 3! = 3 * 2 * 1 = 6 0! is defined to be
  • 3. Recursive Process (cont.) x = factorial( 4 ); int factorial( int num ) { if ( num == 0 || num == 1 ) 4 replaces each occurrence of num Recursive Process return 1; return num * factorial( num – 1 ); }
  • 4. Base Case 1 int factorial( int num ) 2 { 3 if ( num == 0 || num == 1 ) 4 return 1; 5 return num * factorial( num – 1 ); 6 } Notice that these lines stopped the recursion – without these lines, the function will call itself over and over again (infinite recursion
  • 5. Infinite Recursion 1 int factorial( int num ) 2 { 3 if ( num == 0 || num == 1 ) 4 return 1; 5 return num * factorial( num – 1 ); 6 } If one makes a mistake and inputs a negative number into this function: factorial( -2 ); what will happen? Infinite recursion
  • 6. Guidelines • There must be a base case that stops recursion. • Each recursive call should approach the base case. • The recursive function call should work for the base case. • The recursive function call should work for the case next to the base case. • The recursive function should make logical sense, assuming that the recursive function call inside it does everything it should do
  • 7. Recursion on a Linked List (cont.) bool search( Node *ptr, T& foundItem, T& target) { .if ( ptr == NULL ) return false; if ( ptr->info == target ) { foundItem = ptr->info; return true; } return search( ptr->next, foundItem, target ); } Searching for a Mercedes in the linked list: Car mercedes, foundCar; … bool found = search( start, foundCar, mercedes );