principles of programming chapter 11: recursive function in this chapter, you will learn about ...

12
Principles of Programming Chapter 11: Recursive Function In this chapter, you will learn about Recursion function 1 NI S1 2009/10

Upload: jordan-thompson

Post on 01-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Chapter 11: Recursive Function In this chapter, you will learn about

Recursion function

1NI S1 2009/10

Page 2: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Introduction Recursive function is also a form of function.

Therefore, it has to be declared (prototype), defined and called just like other functions.

Recursive function is a function that calls itself. For example:

void power(x, y){

power(x, y-1);

}

2

Page 3: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Introduction Characteristics of problems that can be solved using

recursive function: One or more simple cases of the problem have a

straightforward, nonrecursive solution. The other cases can be redefined in terms of problems

that are closer to the simple cases.

3

Page 4: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Simple recursion When a recursive function is called,

The function will solve the simplest case of the problem (base case). When the simplest case is given as an input, the function will immediately return with an answer.

However, if a more complex input is given, a recursive function will divide the problem into 2 pieces: a part that it knows how to solve (base case) and another part that it does not know how to solve.

4

Page 5: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Simple Recursion The part that it does not know how to solve always

resembles the original problem, but of a slightly simpler version.

Therefore, the function calls itself to solve this simpler piece of problem that it does now know how to solve. This is called the recursion step.

The recursion step is done until the problem converges to become the simplest case.

This simplest case will be solved by the function which will then return the answer to the previous copy of the function.

The sequence of returns will then go all the way up until the original call of the function finally return the result.

5

Page 6: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Simple recursion Example problem: Multiplication of n by m.

Assuming n = 6 and m = 3. Since any number when multiplied by one gives back

the number, we can just solve 6 x 1.

1. Multiply 6 by 22. Add 6 to the result of 1.

Split further …

1. Multiply 6 by 21.1 Multiply 6 by 11.2 Add 6 to the result of 1.1

2. Add 6 to the result of problem 1

6

Page 7: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Simple Recursion cont… Any problem that can be solved recursively can also

be solved iteratively (using loop). Recursive functions are slow and takes a lot of

memory space compared to iterative functions So why bother with recursion? There are 2 reasons:

Recursion approach more naturally resembles the problem and therefore the program is easier to understand and debug.

Iterative solution might not be apparent.

7

Page 8: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Example 1 - xy

In this example, we want to calculate x to the power of y (i.e. xy)

If we analyze the formula for xy, we could see that xy could be written as (x being multiplied to itself, y times).

An example is 24, which can be written as24 = 2 x 2 x 2 x 2 (in this case, x = 2, y = 4)

24 could also be rewritten as24 = 21 x 23 where 21 = 2 (i.e the number itself)

Therefore, we could divide the problem into two stage: Simplest case: when y = 1, the answer is x Recursive case, we need to solve for x * x(y-1)

8

Page 9: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Example 1 - xy

#include <stdio.h>double XpowerY(double,int);

int main(void){

double power, x;int y;

printf("Enter the value of x and y:\n");scanf("%lf%d",&x,&y);

power = XpowerY(x,y);

printf("%.2f to the power of %d is %.2f\n\n",x,y,power);return(0);

}

double XpowerY(double x, int y){

if (y ==1 )return x;

elsereturn x * XpowerY(x, y-1);

}9

Enter the value of x and y:232.00 to the power of 3 is 8.00Press any key to continue

Page 10: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Example 2 - Factorial Analysis:

n!= n * (n-1) * (n-2) * (n-3) * (n-4) ……… * 1 n! could be rewritten as n * (n-1)! Example:

5! = 5 * 4 * 3 * 2 * 1 = 5 * (4)!, where n = 5 Fact: 0! Or 1! is equal to 1

Therefore, we could divide this problem into two stages for n!: Simplest case: if (n <= 1), answer is 1 Recursive case: we need to solve for n * (n-1)!

10

Page 11: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Example 2 - Factorial#include <stdio.h>double fact(double);

int main(void){

double n, result;

printf("Please enter the value of n:");scanf("%lf",&n);

result = fact(n);

printf(" %.f! = %.2f\n",n,result);

return(0);}

double fact(double n){

if (n <= 1)return 1;

elsereturn n * fact(n-1);

}

11

Enter the value of n: 33! = 6.00Press any key to continue

Page 12: Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10

Principles of Programming

Summary In this chapter, you have learnt:

Recursion function

12