CSE 341 -- Parameter Passing Examples

December 5, 1995


The following are intended to help you understand the difference between the following types of parameter passing: 1) call by value, 2) call by result, 3) call by value-result, 4) call by reference, 5) call by name.
  1. int glob;
    
    void Squid(int arg)
    {
       glob += 2;
       arg -= 1;
       printf("%d ", glob);
    }
    
    main()
    {
       glob = 2;
       Squid(glob);	
       printf("%d ", glob);
    }
    
    What is printed if we are using call by value? Call by value result? Call by reference?
    call by value:        4 4
    call by value-result: 4 1
    call by reference:    3 3
    
  2. int arr[2];
    int glob;
    
    void Clam(int arg)
    {
       glob += 1;	
       arg += 50;
    }
    
    void main(void)
    {
       arr[0] = 100;
       arr[1] = 200;
       
       glob = 0;
    
       Clam(arr[glob]);
       /* CHECKPOINT */
    }
    
    What does the array arr contain when we reach the CHECKPOINT if we are using call by reference? Call by name?
    call by reference: { 150, 200 } 
    call by name:      { 100, 250 }