Other things in function call + return
 

Consider a function in a high level programming language:

 /* ------------------------------------------------------
    sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1]) 
    ------------------------------------------------------ */ 
 int sumRange( int[] A, int a, int b )           
 {
     int i, s;

     s = 0;
     for (i = a, i < b; i++)
        s = s + A[i];
     return(s);
 }
   

There are 3 things about functions that we still need to discuss...

There are 3 more things to discussed in function call + return
 

(1):   a function can receive parameters:

 /* ------------------------------------------------------
    sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1]) 
    ------------------------------------------------------ */ 
 int sumRange( int[] A, int a, int b )           
 {
     int i, s;

     s = 0;
     for (i = a, i < b; i++)
        s = s + A[i];
     return(s);
 }
   

Questions:   (a) how are parameters passed to a function and (2) how to access them ?

There are 3 more things to discussed in function call + return
 

(2):   a function can return a value:

 /* ------------------------------------------------------
    sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1]) 
    ------------------------------------------------------ */ 
 int sumRange( int[] A, int a, int b )           
 {
     int i, s;

     s = 0;
     for (i = a, i < b; i++)
        s = s + A[i];
     return(s);
 }
   

Questions:   (a) how does a function return a value and (2) how to use the return value ?

There are 3 more things to discussed in function call + return
 

(3):   a function can have local variables (that are only accessible within the function body):

 /* ------------------------------------------------------
    sumRange(A,a,b): returns (A[a] + A[a+1] + ... + A[b-1]) 
    ------------------------------------------------------ */ 
 int sumRange( int[] A, int a, int b )           
 {
     int i, s; // Local variables 

     s = 0;
     for (i = a, i < b; i++)
        s = s + A[i];
     return(s);
 }
   

Questions:   (a) how to create local variables and (2) how to use the local variables ?

No new assembler instructions needed !

  • Good news:

    • We have learned all the necessary assembler instructions to implement:

      • Parameter passing
      • Returning a value
      • Allocating local variables

    I.e.::

    • There is NO additional assembler instructions needed to:

      • Pass parameters to functions
      • Return a function value
      • Allocate local variables for function

  • How to implement parameter passing, returning a value and allocating local variables:

    • Organize (using the registers or the runtime (program) stack) !      

Prelude to the discussion - terminology: caller and callee

Terminology:

  • Caller (= calling method)

      • Caller = the method that calls another method


  • Callee (= called method)

      • Callee = the method that got called (by another method)


  • Example: the caller and callee in the call f2(x,4):

       caller = f1                      callee = f2
      -------------                  ---------------------
      void f1( )                      int f2(int a, int b)
      {                               {
          int x, y;                       int z = a*a + b*b;
    
          y = f2(x, 4);                   return z;
      }                               }
    

How is parameter passing implemented ?
 

  • Parameter passing is implemented by:

    • An agreement (= contract) between

      1. The caller (= calling method)             and       

      2. The callee (= called method)


  • The caller function and the callee function must agree on:

      1. Where (= location) to pass (= store) the value of each parameter              

      2. How (= by value or by reference) to pass each parameter:

        • Pass by value              or
        • Pass by reference

        (Note: there are other (lesser known) parameter passing methods...)

Places (= locations) that you can use to pass parameters, and store local variables
 

Where do you pass parameters and store local variables:

  1. Registers (in the CPU):

      • Advantage:       simple to use and understand

      • Disadvantage:   will only work with leaf functions    


  2. Program (runtime) stack (in the memory):

      • Advantage:         will work with any (leaf + non-leaf) function    

      • Disadvantage:   more difficult to use and understand    

I will discuss method 1 (using registers) first...

How is returning a value implemented
 

How does the callee function pass a return value to the caller function:

  • Returning a value is also implemented by:

    • An agreement between

      1. The caller method             and       

      2. The callee method

  • The caller function and the callee function must agree on:

      • Where (= location) to pass (= store) the return value

Because function returns a single value:

  • The return location used by compilers is usually a register     (Often: register r0)