Recursive functions.

Slides:



Advertisements
Similar presentations
We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design.
Advertisements

Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
ICS103 Programming in C Lecture 11: Recursive Functions
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Dr Zhang Fall 2014 Fordham University
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
The Fibonacci Sequence
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Program Development and Design Using C++, Third Edition
“Toy” Model: Fibonacci The Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, known as Fibonacci.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion CENG 707.
Topic 6 Recursion.
Recursion what is it? how to build recursive algorithms
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
5.13 Recursion Recursive functions Functions that call themselves
RECURSION.
Chapter 15 Recursion.
Week 6 – Functions (2) PGT C Programming.
Review: recursion Tree traversals
Week 4 – Repetition Structures / Loops
Module 4 Functions – function definition and function prototype.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions Dr. Sajib Datta
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Functions.
Functions Dr. Ashish Sasankar. In programming, a function is a segment that groups code to perform a specific task. A C program has at least one function.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Control Structures Lecture 7.
Recursion Output Input
Recursive Definitions
Recursion Chapter 11.
Programming application CC213
Functions Recursion CSCI 230
A function with one argument
Stack Frames and Functions
Introduction to Problem Solving and Programming
1A Recursively Defined Functions
The structure of programming
The structure of programming
ICS103 Programming in C Lecture 11: Recursive Functions
Thinking procedurally
Recursion: The Mirrors
Presentation transcript:

Recursive functions

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.

Recursion(cont.)

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;

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;

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.

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.

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...

Fibonacci's Rabbits

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;

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

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.