Electron microscopy
 
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 ) ...".

Function and Argument list

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.

Correct (work) Wrong (Does not work)

number EMadd( number EMa, number EMb )
{
return EMa + EMb
}
number EMc
number EMd
EMc = 1
EMd = 2

result ("The sum of EMa and EMb is: " + EMadd (EMc, EMd))
Script file

number EMadd( number EMa, number EMb )
{
return EMa + EMb
}
EMa = 1
EMa = 2

result ("The sum of EMa and EMb is: " + EMadd (EMa, EMb))

void MyEMFunction( number &EMx )

{
result( "x is " + EMx + "\n" )
EMx = 2
result( "x is " + EMx + "\n" )
}

number EMa = 1
MyEMFunction( EMa )

result( "After calling MyEMFunction() a is " + EMa + "\n" )
Script (see Figure 1152b)

number EMa = 1
MyEMFunction( EMa )

void MyEMFunction( number &EMx )

{
result( "x is " + EMx + "\n" )
EMx = 2
result( "x is " + EMx + "\n" )
}

result( "After calling MyEMFunction() a is " + EMa + "\n" )
// The differences are just the order of the lines in red above and in green on left. Namely, any function (e.g. "MyEMFunction( number &EMx )" here) has to be defined (e.g. "void MyEMFunction( number &EMx )" here ) before it is called and used (e.g. in red above).

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.

Function of & in a function

Figure 1152b. Function of & in a function. (Script)

The general form of C++ functions is:
       return-type function-name(parameter list)
       {
       .
       . body of the function
       .
       }

This function has the properties below:
       i) If it does not return a value, its return type is then void. For instance, the example in Figure 1152c 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 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:
                                                       Return statement when void is used

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.
       iii) Every function must have a name, and after the name is a parenthesized parameter list.
       iv) The body of the function is composed of the C++ statements that define what the function does.
       v) The function terminates and returns to the calling procedure when the closing curly brace is reached or when a return statement is encountered. Example

Table 1152b. Example of image functions DM scripts.

Script Note

Script file

functions DM scripts Script file
◆ Different from the script above is that this script has involved conditions so that the resulting images are different at different conditions

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.

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:
       ◆ Return type
       ◆ The type of its parameters
       ◆ The number of its parameters

The prototypes allow the software compiler to perform three important operations:
       ◆ They tell the compiler to generate what type of code when a function is called.
       ◆ They allow the software to figure out and report any illegal type conversions between the type of arguments which are used to call the function and the type definition of its parameters.
       ◆ They allow the compiler to detect differences between the arguments used to call the function and the parameters in the function.

The general form of a function prototype is:
       type func-name(type parm_name1, type parm_name2,...,
                     type parm_nameN);

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.

Table 1152c. Call-by-value versus call-by-reference for argument passing. (Refer to page1123)
Call-by-value
Call-by-reference
Script file

 
Result output: 
   
 
Script file (refer to page1122)
Call-by-reference
 
Result output:
 

 

 

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