Passing variables as parameters to functions
 

Parameters of functions:

  • Functions can receive 2 kinds of parameters:

      1. Constants: f( 4 )           
      2. Variables:   f( x )

Constants can only be passed (= stored) by value
I.e.:   you can only copy the value of the constant to the parameter store location

Variables can by passed in different ways to a function !!!
The 2 common ways are:   (1) by-value and (2) by-reference

How to pass a variable parameter by value
 

The caller function and the callee function will coorperate as follows:

  1. The caller function makes a copy of the value of the variable and give it to the callee function


  2. The callee function access the copy of the value of the parameter variable

    The value in the original variable cannot be updated by the callee function

    (Because the callee function does not know the location of the variable !)

All high level programming languages provides/implements the pass-by-value mechanism

The effect of passing a variable parameter by value - example

Pass-by-value illustrated using C++:

    #include <iostream>
     
    using  namespace  std;
     
    void  f(int x)   // Var x is passed by value
    {
       x = x + 1;
    }
     
    int main(int argc, char *argv[])
    {
       int k = 7;
     
       cout << "before f( ): " << k << endl;    // Prints: 7
       f(k);
       cout << "after  f( ): " << k << endl;    // Prints: 7  
    }
   

DEMO:   /home/cs255001/demo/tmp/demo.C

How to pass a variable parameter by reference
 

The caller function and the callee function will coorperate as follows:

  1. The caller function makes a copy of the (memory) address of the variable and give it to the callee function


  2. The callee function will access the value of the parameter variable through the given memory address

    The value in the original variable will be updated by the callee function (through the given address !)

    (Because the callee function (always) access (= load + store) through the given memory address of the variable !)

C++ and Pascal are programming languages that provides/implements pass-by-reference
Java does not !!!

The effect of passing a variable parameter by reference - example

Pass-by-refernce illustrated using C++:

    #include <iostream>
     
    using  namespace  std;
     
    void  f(int & x)   // Var x is passed by reference
    {
       x = x + 1;
    }
     
    int main(int argc, char *argv[])
    {
       int k = 7;
     
       cout << "before f( ): " << k << endl;    // Prints: 7
       f(k);
       cout << "after  f( ): " << k << endl;    // Prints: 8  
    }
   

DEMO:   /home/cs255001/demo/tmp/demo.C

Compiler and parameter passing mechanism

  • The compiler knows whether a parameter is passed-by-value or passed-by-reference by processing the function definition:

       void f( int x )   // indicates to C++ compiler that 
                         // x is passed-by-value   
      
       void f( int & x ) // indicates to C++ compiler that 
                         // x is passed-by-refrence  
      

  • A parameter that is passed-by-value will read/write the value in the parameter storage location (e.g.: in the stack)

  • A parameter that is passed-by-reference will read/write the value in the original storage location through the address (= reference) that was passed.

These are the defined behavior of pass-by-value and passed-by-reference that is implemented by the compiler !!