Electron microscopy
 
Recursion in C++ and DM
- Practical Electron Microscopy and Database -
- An Online Book -
Microanalysis | EM Book                                                                                   http://www.globalsino.com/EM/        


=================================================================================

 

Recursion is sometimes called circular definition, which is the computing process of defining something in terms of itself and is the process of a function calling itself:
         i) Process:
               i.a) A recursive call does not make a new copy of the function, but just the same functions are called recursively.
               i.b) Only the values of the local variables and parameters are new and are allocated storage on the stack.
               i.c) The function code is executed with these new variables from the start.
               i.d) The old local variables and parameters are removed from the stack as each recursive call returns.
               i.e) The execution resumes at the point of the function call inside the function.
               i.f) Most recursive routines do not significantly reduce the code size of the program.
         ii) Disadvantages:
               ii.a) Most recursive routines may execute slightly slower than their iterative setup due to the additional function calls.
               ii.b) Too many recursive calls to the functions may cause a stack overrun.
               ii.c) It is possible that the stack of the storage for function parameters and local variables will be exhausted because each new call creates a new copy of these variables.
         iii) Advantages:
               iii.a) Recursion can be used to create clearer and simpler versions of several algorithms than those produced with their iterative equivalents.
               iii.b) Some problems, e.g. artificial intelligence, seem to lend themselves to recursive solutions.
               iii.c) Some users find it easier to think recursively rather than iteratively.
         iv) Coding:
               iv.a) A conditional statement, such as an IF statement, must be included to force the function to return without execution of the recursive call. If the conditional statement is not provided, then once the function is called, it will never return. This is a very common mistake in programming.

Table 1125 shows comparison between recursion and iteration applications in C++ and DM.

Table 1125. Examples of recursion application in C++ and DM.

DM script Note
Computes the factorial (series) of an integer with recursive and iterative methods.
Example

This script is to compute the factorial of an integer with recursive and iterative methods

Lines 10 to 16: Recursive version. When EMfactr( ) is called with an argument of 1, the function returns 1, while it returns the product of EMfactr(n–1)*n if n > 1. To evaluate this expression, EMfactr( ) is called with n–1. This happens until n equals 1 and the calls to the function begin returning.

Line 13: "return (1)" has the same function as "return 1"

Lines 18 to 24: Iterative (nonrecursive) version. It uses a loop
starting at 1 and then progressively multiplies each number by the moving product.

Line 31: use the recursive version above

Line 33: use the use iterative version version above

Note that creating recursive functions is often difficult for a beginner. However, over time, the user will grow more accustomed to using such functions.

 

=================================================================================