Sunday, 1 October 2017

How to write a Recursive Code

The general approach to writing a recursive function is listed in the following sequence:

a. Write the function header so you sure what the function will do and how it will be called. Identify unit of measure for the size of the problem the function or procedure will work on. Then, pretend that the task is to write a function that will work on problems of all sizes.

b. Decompose the problem into sub-problem. Identify clearly the non-recursive case of the problem. Make it as small as possible. The function will nearly always begin by testing for this non-recursive case, also known as the base case or end condition.


c. Write recursive calls to solve those sub-problems whose form is similar to that of the original problem.

d. Write the code to combine, enhance, or modify the results of the recursive calls, if necessary, to construct the desired return value or create the desire side effect.

e. Write the end conditions to handle any situations that are not handled properly by the recursive portion of the program.  


Thanks
Mukesh Rajput 

No comments:

Post a Comment

Thanks
Mukesh Rajput