Program Development Styles and Basics of C 21
The common flowchart for any kind of recursive function is as per Fig. 1.26.
Preliminary part: The use of this block is to store the local variables, formal arguments, and return
address. The end-part will restore these data. Only recently saved arguments, local variables, and
return address are restored. The variables last saved are restored first.
Body: If the test condition is satisfied it performs the complete processing and control passes to the
end-block. If not partial processing is performed and a recursive call is made. The body also contains
call to itself. One or more calls can be made. Every time a recursive call is made, the preliminary
part of the function saves all the data. The body also contains two processing boxes, that is, partial
processing and complete processing. In some programs the result can be calculated by only complet
processing. For this the recursive call may not be required for example, if we want to calculate the
factorial of 1. The factorial of 1 is 1. For this it is needless to call a function recursively. It can be
solved by transferring control to complete processing box.
In another case, if 5 is given for factorial calculation, the factorial of 5 can be calculated in one
step. Hence, the function will be called recursively. Every time one step is solved, that is, 5*4*3, and
so on. Hence, it is called partial processing.
Depth of recursion: The recursion function calls itself infinite times. If we want to calculate the
factorial of 5 then we can easily estimate the number of times the function would be called. In this
case, we can determine the depth of the recursive function. In a complex program it is difficult to
determine the number of calls of the recursive function.
1.10 RECURSION VERSUS ITERATION
We have studied both recursion and iteration. They can be applied depending upon the situation.
Table 1.1 explains the differences between recursion and iteration.
Table 1.1 Differences between recursion and iteration
Recursion
Iteration
Recursion is the term given to the
mechanism of defining a set or
procedure in terms of itself.
The block of statements executed
repeatedly using loops.
A conditional statement is required in
the body of the function for stopping
the function execution.
The iteration control statements itself
contain statement for stopping the
iteration. At every execution, the
condition is checked.
At some places, use of recursion
generates extra overhead. Hence, better
to skip when easy solution is available
with iteration.
All problems can be solved with
iteration.
Recursion is expensive in terms of
speed and memory.
Iteration does not create any overhead.
All the programming languages
support iteration.
1.11 OVERVIEW OF COMPILERS AND INTERPRETERS
A program is a set of instructions for performing a particular task. These instructions are just like
english words. The computer processes the instructions as Is and Os. A program can be written in
assembly language as well as high level language. This written program is called 'source program'.
The source program is to be converted to the machine language, which is called 'object program'.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.138