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


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

 

An overloading function is two or more distinct functions with the same name, but different argument lists (namely, the parameter declarations of the functions are different). Such argument lists must differ either in number of arguments or in type. In other words, the function overloading is the quality that allows one function to be implemented two or more different ways, and each way performs a separate task. In this case, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading. Function overloading is actually one way that C++ supports polymorphism and is one of C++’s most exciting features. DM also supports function overloading. Table 1124 lists some examples of overloading functions in DM. Through the application of polymorphism, more function names to remember have been reduced to only one. Polymorphism is one of three major facets of object-oriented programming. As applied to C++ as well as DM, polymorphism is the term used to describe the process by which different implementations of a function can be accessed via the same name. For this reason, polymorphism sometimes is characterized by the phrase "one interface, multiple methods".

Table 1124. Examples of overloading functions in DM.

Script
Note
overloading functions
script file

Three overloading functions here.

add () is overloaded three times. The first version takes one number parameter, and the second version requires two number parameters, and the third version has three number parameters. Because the parameter list for each version is different, the DM compiler is able to call the correct version of each function. In general, to overload a function, you simply declare different versions of it.

overloading functions in DM
script file

Output of the script:
   overloading functions

EMfunc( ) is overloaded two times.

overloading functions
script file

Output of the script:
overloading function

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

This program creates three similar but different functions called EMabs(), each of which returns the value of its argument. The compiler knows which function to use in each given case due to the corresponding argument. The value of overloading is that it allows its related set of functions to be accessed using a common function name "EMabs". Therefore, the name EMabs represents the general action that is being performed. It lets the compiler to choose the correct specific version for a particular circumstance.


script file

This script does not work because it attempts to redeclare the body of the function (Lines 14-18 and Lines 20-24 redeclare the body of the function EMabs() presented in Lines 8-12). Note the difference of argument names (i, d, and f) does not mean that the functions are different.

The error message below is shown when the script is executed:
Error in overloading function

The DM compiler uses the type and/or number of arguments as its guide to determining which version of an overloaded function to call. Therefore, overloaded functions must differ in the type and/or number of their parameters. While overloaded functions may have different return types, and thus the return type alone is not sufficient to distinguish two versions of a function.

When you overload a function, each version of that function can perform any activity you want. That is, no rule frames that overloaded functions must relate to one another. However, in good practice, function overloading implies a relationship. Therefore, while the same name can be used to overload unrelated functions, you should not. For instance, you could use the name "sqr" to create functions that return the square of a number and the square root of the number. However, these two operations are fundamentally different, and thus applying function overloading in this manner defeats its original purpose. One of such examples are the functions "EMabs" in Table 1124. "abs" normally is used to return its absolute value. In fact, programming in this manner is considered to be really bad style! In programming, you should overload only closely related operations.

In practice, it is possible to create a situation where the compiler is unable to choose between two (or more) correctly overloaded functions. Such a situation is said to be ambiguous. Ambiguous statements are programming errors, and programs containing ambiguity will not compile.

 

 

 

 

 

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