Section 9.5: Subroutines (Frame 8)                     [prev][home][next]

If the subroutine has local variables (our example doesn't), these have to be put back to their initial values to avoid surprises. Programmers are always admonished to assign initial values to their variables and in older compilers this wasn't done automatically so it was possible to get "garbage values" in assembler or FORTRAN variables upon startup. Newer languages always initialize or warn you (as does Java). In assembler, we have to handle every low level detail ourselves! So the following fragment of subroutine must be code carefully:

int somefunc (int x)
{
     int sum = 0;
     int count = 0;
...
}

In this example, sum and count are local variables and must be set to 0 every time somefunc is called.