Electron microscopy
 
Argument Passing in DM and C++
== Call-by-value versus Call-by-reference ==
- Practical Electron Microscopy and Database -
- An Online Book -
Microanalysis | EM Book                                                                                   http://www.globalsino.com/EM/        


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

 

There are two ways that a computer language can pass an argument to a subroutine:
          Call-by-value. In this method, the value of an argument is copied into the formal parameter of the subroutine. Therefore, changes made to the parameters of the subroutine will not affect the arguments which is used to call it.

              C++, by default, uses the call-by-value method for passing arguments. Therefore, in general, code inside a function cannot alter the arguments which are used to call the function.

Table 1123a. Call-by-value method for passing arguments.

Script Note
call-by-value method for passing arguments
Script file

The value of the argument to EMsquare(t) in Line 14, 10, is copied into the parameter EMx in Lines from 7 to 10.

When the assignment EMx = EMx*EMx takes place, the only thing, which is modified, is the local variable EMx. However, the variable t, which is used to call EMsquare( ), will still have the value 10 and is unaffected by the operations inside the function. Therefore, the output will be 100.

          Call-by-reference. In this way, a subroutine can be passed arguments. The address of an argument (not its value) is copied into the parameter. Inside the subroutine, this address is used to access the actual argument which is specified in the call. Therefore, the changes made to the parameter will affect the argument used to call the subroutine.

              Even though C++'s default passing convention is call-by-value, it is still possible to manually create a call-by-reference by passing the address of an argument, e.g. a pointer to the argument, to a function. Then, it is possible for code inside the function to change the value of the argument outside of the function. However, it is necessary to declare the parameters as pointer types. Note that these pointer types for call-by-reference is not available in DM interface.

Table 1123b. Call-by-reference method for passing arguments.

Script Note
Call-by-reference method for passing arguments
Script file

swap() is defined using call-by-reference, not call-by-value. Therefore, it exchanges the two arguments it is called with

x and y are reference parameters

The output results will be the same if Line 18 is changed to swap(EMi, EMj), namely the order of EMi and EMj is not a matter

The argument swap(EMj, EMi) in Line 18 is used to call the function in Line 6

 

Table 1123c. Call-by-value versus call-by-reference for argument passing.

Call-by-value
Call-by-reference
Script file

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

 

 

 

 

 

 

 

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