0% found this document useful (0 votes)
61 views11 pages

(12-1) Recursion H&K Chapter 9: Instructor - Andrew S. O'Fallon Cpts 121 (April 7, 2014) Washington State University

This document discusses recursion and recursive functions. It defines a recursive function as a function that calls itself, either directly or indirectly. It explains that problems solved through recursion have simple base cases and are defined in terms of problems closer to the base cases through recursive calls. An example of a recursively defined multiplication function is provided and its evaluation is traced step-by-step. The document concludes by mentioning that more recursive examples will be covered in the next lecture.

Uploaded by

T uohz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views11 pages

(12-1) Recursion H&K Chapter 9: Instructor - Andrew S. O'Fallon Cpts 121 (April 7, 2014) Washington State University

This document discusses recursion and recursive functions. It defines a recursive function as a function that calls itself, either directly or indirectly. It explains that problems solved through recursion have simple base cases and are defined in terms of problems closer to the base cases through recursive calls. An example of a recursively defined multiplication function is provided and its evaluation is traced step-by-step. The document concludes by mentioning that more recursive examples will be covered in the next lecture.

Uploaded by

T uohz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

(12-1) Recursion

H&K Chapter 9
Instructor - Andrew S. OFallon
CptS 121 (April 7, 2014)
Washington State University

What is a Recursive Function?


A function

that calls itself either directly or


indirectly through another function
For example:
int recursive_function (int r, int s)
{

recursive_function (r, s-1) /* recursive call */

C. Hundhausen, A. OFallon

Nature of Recursion (1)

Problems that may be solved using


recursion have these attributes:

One or more simple cases have a


straightforward, non-recursive solution
The other cases may be defined in terms of
problems that are closer to the simple cases
Through a series of calls to the recursive
function, the problem eventually is stated in
terms of the simple cases

C. Hundhausen, A. OFallon

Nature of Recursion (2)


As

described by Wirth:

The power of recursion evidently lies in the


possibility of defining an infinite set of objects by a
finite statement. In the same manner, an infinite
number of computations can be described by a
finite recursive program, even if this program
contains no explicit repetitions.

C. Hundhausen, A. OFallon

Nature of Recursion (3)


A divide

and conquer approach


A fresh copy of a function goes to work on a
similar, but simpler problem than the original

C. Hundhausen, A. OFallon

Properties of Recursion

Recursive solutions have at least two cases:

The simple or base case(s)


The recursive case(s) or step(s)

C. Hundhausen, A. OFallon

Example of Recursive Solution

This is an example provided on p. 528 of the Hanly & Koffman text. This example
performs multiplication through addition.

int multiply (int m, int n)


{
int ans;
if (n == 1)
{
ans = m; /* simple or base case */
}
else
{
ans = m + multiply (m, n 1); /* recursive step */
}
}

return ans;

C. Hundhausen, A. OFallon

Evaluation of Recursive Multiply


// 1st call
multiply (2, 3)

. .

m = 2,
n=3

. .

// 2nd call

. .

multiply (2, 2)
m = 2,

. .

n=2

. .

// 3rd call

. .

multiply (2, 1)

ans = 6

. .

ans = 4

ans = 2 +
2=4

ans = 2

. .

m = 2,

// Base case reached

n=1

ans = 2

C. Hundhausen, A. OFallon

ans = 2 +
4=6

ans = 2

Next Lecture
More

recursive examples

C. Hundhausen, A. OFallon

References
J.R.

Hanly & E.B. Koffman, Problem Solving


and Program Design in C (7th Ed.), AddisonWesley, 2013
P.J. Deitel & H.M. Deitel, C How to Program
(5th Ed.), Pearson Education , Inc., 2007.
N. Wirth, Algorithms + Data Structures =
Programs, Prentice-Hall, 1976

10

C. Hundhausen, A. OFallon

Collaborators
Chris

11

Hundhausen

C. Hundhausen, A. OFallon

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy