next up previous
Next: Project on Machine Description Up: Projects on gcc Intermediate Previous: Level 3


Level 4

Add function definition, call and return mechanism to the language in Level 3.

Functions are basic named computation units with their own data and control structures. As such at least their data and control must be ``separated at specification time'' from the main structure, but their execution must be linear at the call points. This support must be implemented by the compiler.

Now we have reasonably complete programs:

  1. int main ()
    {
      int n = 5; int c; int x;
    
      c = factorial (n);
      x = square (c);
    
      return x;
    }
    
    int factorial (int in)
    {
      int ans;
    
      if (in == 0)
        ans = 1;
      else
        ans = in * factorial (in - 1);
    
      return ans;
    }
    
    int square (int x)
    {
      return (x * x);
    }
    



2006-01-08