Understanding Recursion in Programming Fundamentals

ece 244 n.w
1 / 5
Embed
Share

Explore the concept of recursion in programming fundamentals, where problems are solved by breaking them down into smaller steps. Learn how recursive functions work through examples and dive into recursion on a linked list. Discover the power of recursive algorithms in problem-solving.

  • Recursion
  • Programming
  • Fundamentals
  • Algorithms

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. ECE 244 Programming Fundamentals Lec. 17: Recursion Ding Yuan ECE Dept., University of Toronto http://www.eecg.toronto.edu/~yuan

  2. Recursion A recursive algorithm is one which breaks down problem to be solved into smaller, discrete steps that are solved using the same algorithm A recursive function is an implementation of a recursive algorithm It makes calls to itself to perform each step

  3. Example: Factorial n! = n * (n-1) * (n-2) * 1 24 f(4) = ? 6 f(4) = 4 * f(3) 2 f(3) = 3 * f(2) 1 f(2) = 2 * f(1) f(1) = 1

  4. Recursive Functions int factorial (int n) { if (n==1) return 1; // Basis return (n*factorial(n-1)); // Recursive } int main() { int f = factorial(4); } Return val Stack Frame factorial (1) 1 factorial (2) 2 factorial (3) 6 factorial (4) main() 24 24

  5. Recursion on a Linked List 3 5 23 187 void rprint (Node *p) { if (p == NULL) return; rprint(p->next); cout << p->data; } rprint(head);

More Related Content