Parameter Passing Methods in Programming Languages

Formal and Actual Parameters

To understand this article, it is crucial to understand the difference between formal parameters and actual parameters. Formal parameters are the variables in a subprograms prototype, while actual parameters are the arguments to that subprogram when it is called. Let me demonstrate this with C++ code to make it more clear:

int product(int x, int y) {    // (1)
    return x * y;
}

int main() {

    int x = 2;
    int y = 1;

    int z = product(x, y);     // (2)
}

If you look at the variables in (1), you're looking at the formal parameters. When you look at the variables x and y passed to the subprogram product in (2), you look at the actual parameters.

There are three different semantics models of parameter passing, namely in modeout mode and inout mode. In mode means that the formal parameter can receive data from the corresponding actual parameter, out mode means that the formal parameter can transmit data to the actual parameter, inout mode means that they can do both. Based on these three basic parameter transmission models, programming language designers have developed models to guide the implementation in languages. Here I discuss Pass by Value, Pass by Result, Pass by Value-Result, and Pass by Reference.

 

Pass by Value

Pass by value is an implementation of the in-mode semantics. The formal parameter is initialized by the corresponding actual parameter when a parameter is passed by value. The formal parameter then acts as a local variable in the subprogram. This is typically implemented by copy. In the C++ code above, the parameters are passed by value. x and y are copied into the subprogram product.

 

Pass by Result

Pass by result is an implementation of the out-mode semantics. Here, no value is transmitted to the program. The formal parameter acts as local variable as in the pass by value implementation, but just before the subprogram ends the value of the formal parameters are transmitted to the caller's actual parameter. Pass by result is usually implemented by copy.

 

Pass by Value-Result

Pass by value-result is an implementation of the inout-mode semantics. It is a combination of pass by value and pass by result. The formal parameter is initialized by the corresponding actual parameter, and the formal parameter then acts as a local variable in the subprogram. When the subprogram reaches its end, the formal parameter transmits its value back to the actual parameter.

 

Pass by Reference

Pass by reference is also an implementation of the inout-mode semantics. Instead of copying the values like pass by value-result does, pass by reference transmits an access path to the called subprogram. The access path is usually an address. You might be familiar with the C++ referencing operator sometimes used in subprograms parameter lists, &. This is telling the computer that you want to pass the parameter by reference.

void lobster(int& ham); // pass by reference

 

Author

authors profile photo

Articles with similar tags

thumbnail
Type checking in programming languages

Type checking prevents type errors to happen.

By Frogitecture