void main( ) // I omitted the 'public static' for simplicity
{
int x = 4, y = 7, z;
z = F(x, y); // Pass parameters x, y and receive return value
}
int F(int a, int b) // I omitted the 'public static' for simplicity
{
int s, t; // Local variables
....
return(77);
}
|
The subroutine call statement:
z = F(x, y); // Subroutine call statement
|
packs a lot of meaning in a high level programming language
We are here to spell out what exactly happens in a subroutine invocation (call) statement.
|
|
is:
|
|
So passing parameters, passing return values and allocating memory for local variables can be done using:
|
void main( ) // I omitted the 'public static' for simplicity
{
int x = 4, y = 7, z;
z = F(x, y); // Pass parameters x, y and receive return value
}
int F(int a, int b) // I omitted the 'public static' for simplicity
{
int s, t; // Local variables
....
return s;
}
|
The subroutine call statement:
z = F(x, y); // Subroutine call statement
|
packs a lot of meaning in a high level programming language
We are here to spell out what exactly happens in a subroutine invocation (call) statement.
|
z = F( x, y );
|
main( ) passes its variable x and y to F( )
We can pass these parameters by copying the values in these variables in some registers
For example:
|
|
Schematically explained:
|
|
void main( ) // I omitted the 'public static' for simplicity
{
int x, y, z;
z = squaresum(x, y); // Pass parameters x, y and receive return value
}
/* --------------------------------------
Returns: a^2 + (a+1)^2 + ... + b^2
-------------------------------------- */
int squaresum(int a, int b) // I omitted the 'public static' for simplicity
{
int i, s; // Local variables
s = 0;
for ( i = a; i <= b; i++ )
s = s + square(i);
return s;
}
int square(int x)
{
return(x*x);
}
|