Download presentation
Presentation is loading. Please wait.
1
Recursive functions
2
Recursion Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
3
Recursion(cont.)
4
Number Factorial The following example calculates the factorial of a given number using a recursive function #include <stdio.h> int factorial(unsigned int i) { if(i <= 1) return 1; } return i * factorial(i - 1); int main() int i = 15; printf("Factorial of %d is %d\n", i, factorial(i)); return 0;
5
Sum of Natural Numbers Using Recursion
#include <stdio.h> int sum(int n); int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum=%d", result); } int sum(int num) if (num!=0) return num + sum(num-1); // sum() function calls itself else return num;
6
Sum of Natural Numbers Using Recursion
Initially, the sum() is called from the main() function with number passed as an argument. Suppose, the value of num is 3 initially. During next function call, 2 is passed to the sum() function. This process continues until num is equal to 0. When num is equal to 0, the if condition fails and the else part is executed returning the sum of integers to the main() function.
8
Recursion(cont) The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop. Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
9
Fibonacci Series Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was...
10
Fibonacci's Rabbits
11
Method 1: Dynamic programming
#include <stdio.h> int main() { int i, n, t1 = 0, t2 = 1, nextTerm = 0; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci Series: "); for (i = 1; i <= n; ++i) { // Prints the first two terms. if(i == 1) printf("%d, ", t1); continue; } if(i == 2) { printf("%d, ", t2); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; printf("%d, ", nextTerm); return 0;
12
Method 2: recursion #include <stdio.h> int fibonacci(int i) {
if(i == 1) return 1; } if(i == 2) return fibonacci(i-1) + fibonacci(i-2); int main() int i; for (i = 1; i < 10; i++) printf("%d\t\n", fibonacci(i)); return 0; Method 2: recursion
13
Advantages and Disadvantages of Recursion
Recursion makes program elegant and cleaner. All algorithms can be defined recursively which makes it easier to visualize and prove. If the speed of the program is vital then, you should avoid using recursion. Recursions use more memory and are generally slow. Instead, you can use loop.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.