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...
(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 ?
(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 ?
(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 ?
|
Terminology:
|
|
Where do you pass parameters and store local variables:
|
I will discuss method 1 (using registers) first...
How does the callee function pass a return value to the caller function:
|
Because function
returns
a
single value:
|