Return Statement in DM
- Practical Electron Microscopy and Database - - An Online Book - |
||||||||||||
Microanalysis | EM Book http://www.globalsino.com/EM/ | ||||||||||||
================================================================================= | ||||||||||||
The general form of return is: The return statement can be used with or without an associated value: 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. 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 Table 1151a. Examples of the use of return statement.
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: 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.
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.
|
||||||||||||
================================================================================= | ||||||||||||
|
||||||||||||