Functions in DM - Practical Electron Microscopy and Database - - An Online Book - |
|||||||||||||||||
Microanalysis | EM Book http://www.globalsino.com/EM/ | |||||||||||||||||
================================================================================= | |||||||||||||||||
Functions are the building blocks of C++. A function can be a user defined operation. Every C++ function must have a name, and the only function that any C++ program must include is called main( ). A function does not need to return a value (NonValue-Returning). In other programming languages, such a function is called a procedure or a subroutine. In C++-based DM scripts, such a function is identified simply by placing the keyword void where the function’s return type would be. A function in DM has a name, an argument list, a return type, and a body. In C++ and thus in DM, all functions must be declared before they are used. For instance, "result (EMadd (1,2) + "\n")" in Figure 1152a cannot be the first line in the script because this use has to locate after the function EMadd is declared, namely the block for "number EMadd( number EMa, number EMb ) ...". Figure 1152a. Function and argument list. (Script) Table 1152a gives a comparison between correct and wrong DM scripts. The wrong script does not work even though it seems correct. Table 1152a. Example of correct and wrong DM scripts.
A function can have 0 or more arguments (an argument list). If a function has 0 arguments then the keyword void is used in place of argument list. Figure 1152b. Function of & in a function. (Script) The general form of C++ functions is: This function has the properties below: Figure 1152c. Return statement when void is used (without a value). (original script) Void functions prevent
their use in an expression and help head off accidental misuse. In Figure 1152c, since GlobalSinoPower (number EMi, number EMNumber) is declared as void, it then cannot be used in an expression. For instance, the statement with "x = GlobalSinoPower (EMi, EMNumber)" in Figure 1152d is wrong, and will not compile: Figure 1152d. Misuse of an expression when void is used in a function. An error message " Error ... Unable to match this argument list to any existing function" will be shown if the script is executed in DM. ii) If it does return a value, then that value must be of a
type that is compatible with the function’s return type. Table 1152b. Example of image functions DM scripts.
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 In C++ and thus in DM, all functions must be
declared before they are used. This declaration can normally be accomplished by use of the function
prototypes, which specify three things about a function: The prototypes allow the software compiler to perform three important operations: The general form of a function prototype is: It is a good idea to include parameter names in a prototype since the use of the names let the compiler identify any type mismatches by name when an error occurs.
|
|||||||||||||||||
================================================================================= | |||||||||||||||||
|
|||||||||||||||||