Review:   parameter passing mechanisms

  • Recall that the 2 commonly used parameter passing mechanisms are:

      1. Pass-by-value

          • The value of the actual parameter variable is copied into the formal parameter variable

        Without its address, the function cannot update (= change) the actual parameter variable

      2. Pass-by-reference

          • The reference (= memory address) of the actual parameter variable is copied into the formal parameter variable

        Using its address, the function can update (= change) the actual parameter variable !

The parameter passing mechanism used in C

  • Parameter passing mechanism in C:

      • C programs always use the pass-by-value parameter passing mechanism

  • Note:

      • Some expressions in C returns a value of an address

      • When the value of an address is passed (= copied), the effect is like pass-by-reference !

    We will see this phenomenon soon for array variable parameters.  

     

Example that shows C uses pass-by-value

  • Example program that shows that C uses pass-by-value:

       void update( double a )    // a is passed by value 
       {
          printf( "Inside update - before changing a, a = %lf\n", a);
          a = a + 1;
          printf( "Inside update - AFTER  changing a, a = %lf\n", a);
       }
        
       int main( int argc, char* argv[] )
       {
          double a = 3.14;
        
          printf( "Inside main - before calling update(a), a = %lf\n", a);
          update ( a );
          printf( "Inside main - AFTER  calling update(a), a = %lf\n", a);
       }
      

    After the call update( a ), the value of a is unchanged !

DEMO: demo/C/set1/pass-by-value1.c

Why is a pass-by-value parameter not updated

  • This is the situation when the program begins execution:                                                      

    I will show you what happens when the program runs

Why is a pass-by-value parameter not updated

  • When main( ) starts running, it first creates (= allocates) the local variable a:            

     

Why is a pass-by-value parameter not updated

  • In pass-by-value the function call update( a ) will copy the value (3.14) of the variable into the formal parameter of the function:

    Note: the actual parameter and the formal parameter are 2 different variables

Why is a pass-by-value parameter not updated

  • The update( ) function will update its formal parameter variable:                                                      

    Note: the actual parameter is not updated !!!

Why is a pass-by-value parameter not updated

  • When the update( ) function returns, the formal parameter variable is de-allocated:

    End result: the actual parameter is not updated !!!

Looking ahead:   C can pass variable by reference

  • Looking ahead:

      • C has a low level operator & that returns the address of a variable

      • Using the & operator, C programs can pass the (value of the) address of a variable x as follows:

                    &x 

  • This feature will be discussed later in this course.


  • Programming language that can pass parameter by reference:

    • C++ !!!

    See: /home/cs255001/demo/C/pass-by-ref/param.C

Why is a pass-by-reference parameter updated

  • This is the situation when the program begins execution:                                                      

    I will show you what happens when the program runs

Why is a pass-by-reference parameter updated

  • When main( ) starts running, it first creates (= allocates) the local variable a:            

     

Why is a pass-by-reference parameter updated

  • In pass-by-reference the function call update( a ) will copy the address (8200) of the variable into the formal parameter of the function:

    Note: the update( ) will access the value of the parameter variable through its address

Why is a pass-by-reference parameter updated

  • The update( ) function will use the address in the formal parameter to access the actual parameter variable:

    Note: the actual parameter is updated !!!

Why is a pass-by-reference parameter updated

  • When the update( ) function returns, the formal parameter variable is de-allocated:

    End result: the actual parameter is updated !!!