Introduction

C++ has pass by reference and pass by value. In Java, all parameters are passed by value, but when the parameter is an object, it isn’t the object that gets passed, it is its address. To pass objects by value in Java, you must use the Object method clone() to create a copy.

 For programming languages in general, parameters can be one of the following three types:

·         input (import) only

·         output (export) only

·         input/output (import/export)

The Ada programming language tags its parameters as IN, OUT or IN OUT, These were more commonly referred to using terms input and output, but the late Ohio University instructor Rod Lyndon used the terms import and export because he believed students could confuse these parameter types with actual program input and output.

Description

Input (import)

The formal parameter corresponding to an actual argument is held in storage local to the function. The formal parameter holds the evaluated value of the actual argument, which usually (depending on the language) can be a constant, variable, or expression. Changing the formal parameter has no effect on the corresponding actual argument.

Output (export)

The formal parameter corresponding to an actual argument is a reference, i.e. it simply holds an address. The actual argument can only be a variable of type identical to the formal parameter (storage differs among different types). Changing the formal parameter also alters the corresponding actual argument. The actual argument’s value before the call is irrelevant, as its purpose is to be ‘filled in’ during the function’s execution.

Input/Output (import/export)

The formal parameter corresponding to an actual argument is a reference, i.e. it simply holds an address. The actual argument can only be a variable of type identical to the formal parameter (storage differs among different types). Changing the formal parameter also alters the corresponding actual argument. The actual argument’s value before the call is needed, as that value is used in some way in a computation that potentially changes its value.

 

The tables below show the relation between parameter types and parameter passing in C++. Note that passing by const-reference is explained here: http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PARAMS.html

 

            

Pass Method

Parameter Type

Value

Import 

Reference

Export or Import/Export

Const-Reference

Import

 

 

 

 

 

Parameter Type

Pass Method

Import

Any*

Export

Reference

Import/Export

Reference

 

 

 

*Note that a parameter may be passed by reference but not altered in a function. This isn’t ordinarily done; const-reference is used in these situations, where the intent is usually to save memory and computing time.

 

Example

 

Units of measurement lend themselves well to conversions. For example, if someone refers to a unit of time as 75 seconds, most people will convert that to one minute and 15 seconds, as that manner of expression is more familiar to them. Similarly, for 27 inches (2 feet, 3 inches).

 

Suppose we have a box that can perform conversions:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


If we put 75 in for the original units, and 60 (seconds per minute) in for the factor, then the computed units will be 1 (75/60) and the original units will now be 15 (75%60). Here’s the box, with the inside visible:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Both the original units and the factor must have a value when the function is called, since that value is used in a computation. The new units is ‘filled in’ inside the function, and the original units come out potentially altered, since a computation is assigned to that as well.

 

Here’s the C++ function:

 

void Convert(int &OriginalUnit, int &NewUnit, int Factor)

{  NewUnit=OriginalUnit/Factor;

OriginalUnit=OriginalUnit%Factor;

}

 
 

 

 

 

 


OriginalUnit is needed for the computation AND it is assigned. It is import/export.

NewUnit is assigned. Its value is never used, however. It is export only.

Factor’s value is used in computations. It is never assigned (even if it was, that would be silly; unit conversion factors don’t change). It is import only.

 

Here is the Ada version of this function:

Procedure IntegerConvert (OriginalUnits: IN OUT Integer; NewUnits: OUT Integer;Factor: IN Integer) is

  Begin

                NewUnits:=OriginalUnits/Factor;

                OriginalUnits:=OriginalUnits MOD Factor;

  End IntegerConvert;

 
 

 

 

 

 

 

 


Identifying parameter types is simply a matter of understanding what the parameter’s purpose is in the function. Once that has been determined, the task of deciding how to pass the actual argument to that formal parameter is simplified, regardless of language and syntax.