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


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

 


A function ends when the function returns. In C++, this line terminates main( ) and causes it to return the value 0 to the calling process from a function. That is, the return statement halts execution at the point at which it occurs in a function and resumes execution where the function was called.

The general form of return is:
          return value
where,
          "value" is the value being returned.

The return statement can be used with or without an associated value:
           Functions, which are declared as returning a value, must return a value. One example with such return values is shown in Figure 1151a.

Function and Argument list

Figure 1151a. Function and argument list. (Script)

           Only functions, which is declared as void, can use return without a value. In other words, void is used only when a function does not return a value. For void functions, the return statement is mainly used as a program-control device. For instance, the example in Figure 1151b shows when the input number is negative, then the calculation is skipped by the (void) return statement since negative numbers does not have a square root.

Return statement when void is used

Figure 1151b. Return statement when void is used (without a value). (original script)

Unless it is of type void, every function returns a value with an explicitly specified return statement. Therefore, as long as a function is not declared to be void, it should be used as an operand in an expression. Although all non-void functions return values, it is not necessary to use
the values for anything. If there is no assignment specified, the return value is then simply discarded. Table 1151a shows some examples of the use of return statement.

Table 1151a. Examples of the use of return statement.

DM script Note
use of return statement
Script file
The abs( ) functions in Lines 11, 12, and 13 return their absolute values of the arguments.
In Line 11, the return value of abs( ) is assigned to i.
In Line 12, the return value is not actually assigned, but it is used by the RESULT statement.
In Line 13, the return value is lost because it is neither assigned to another variable nor used as part of an expression.

If a non-void function returns due to the occurrence of its closing curly brace, an undefined (i.e., unknown) value is then returned. Due to a quirk in the formal C++ language, a non-void function does not have to actually execute a return statement. For instance, this can happen if the end of the function is reached prior to a return statement encountered.

In practice, a function may contain several return statements. As soon as one matchs, the function returns. For example, this form below is perfectly valid:
          void function()
          {
          // ...
          case 'a': return;
          case 'b': // ...
          case 'c': return;
          if(EMcount<50) return;
          // ...
          }

Table 1151b lists examples of the more than one return statement for the same function.

Table 1151b. Examples of the more than one return statement for the same function.

DM script Note
more than one return statement for the same function
Script file

The EMadd ( ) function uses two return statements (in Lines 17 and 19) to simplify its operation.

The function searches a value of EMt, from 1 to 9 (Line 13), which equals to the input number "EMNumber" (Line 10).

It returns the index (the main stream Line 7 and the lines from Lines 22 to 27), to output a value of "EMadd (EMc, EMd)", for the first matching.

If no match is found, it returns –1 as the value of "EMadd (EMc, EMd)".

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

overloading functions
script file

Output of the script:
overloading function

Lines 8 to 12 defines the abs() function to return an absolute value

It is important to note that having too many returns can mix up the operation of a algorithm and thus confuse its meaning. It is best to use multiple return statements only when they help clarify a function.

 

 

 

 

 

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