11 - ICS103 Programming in C Lecture 11: Recursive...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 11: Recursive Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Introducing Recursive Functions Format of recursive Functions Tracing Recursive Functions Examples Tracing using Recursive Trees
Background image of page 2
3 Introducing Recursive Functions We have seen so far that a function, such as main , can call another function to perform some computation. In C, a function can also call itself. Such types of functions are called recursive functions. A function, f , is also said to be recursive if it calls another function, g , which in turn calls f . Although it may sound strange for a function to call itself, it is in fact not so strange, as many mathematical functions are defined recursively. For example, the factorial function is defined mathematically as: Although less efficient than iterative functions (using loops) due to overhead in function calls, in many cases, recursive functions provide a more natural and simple solutions. Thus, recursion is a powerful tool in problem solving and programming. n (n-1)! , n>1 1, n = 0 n! =
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Introducing Recursive Functions . .. Problems that can be solved using recursion have the following characteristics: One or more simple cases of the problem have a direct and easy answer – also called base cases . Example: 0! = 1. The other cases can be re-defined in terms of a similar but smaller problem - recursive cases . Example: n! = n (n-1)! By applying this re-definition process, each time the recursive cases will move closer and eventually reach the base case. Example: n! (n-1)! (n-2)! . . . 1!, 0!. The strategy in recursive solutions is called
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

11 - ICS103 Programming in C Lecture 11: Recursive...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online