Functions Basic Terminology o Nonvaluereturning functions n known

Functions

Basic Terminology o Non-value-returning functions: n known as “procedures” in Ada, n “subroutines” in Fortran, “void functions/methods” in C/C++/Java n called from a separate statement. o strcpy(s 1, s 2); o Value-returning functions: n known as “non-void functions/methods” in C/C++/Java n called from within an expression o x = (b*b - sqrt(4*a*c))/2*a 2

int h, i; void B(int w) { Function Call int j, k; and Return i = 2*w; w = w+1; o Control returns from a called function in either of two } void A(int x, int y) { ways bool i, j; n void function or B(h); procedure } o At the end of function int main() { body int a, b; o Return statement h = 5; a = 3; b = 2; n Non-void function A(a, b); o Return statement n Has an expression for the } value to be returned 3

Parameters void A(int x, int y) { -------; } int main() { ------; A(a, b); } o An argument is an expression that appears in a function call. o A parameter is an identifier that appears in a function declaration. o Example: n The call A(a, b) has arguments a and b. n The function declaration A has parameters x and y. o Parameter-Argument Matching n Usually by number and by position 4

Parameter Passing Mechanisms o o By By value reference value-result name 5

Pass by Value o The value of the argument is computed at the time of the call and is assigned to the parameter. o So passing by value doesn’t normally allow the called function to modify an argument’s value. o All arguments in C and Java are passed by value. 6

Pass by Reference o Compute the address of the argument at the time of the call and assign it to the parameter. o Since h is passed by reference, its value changes during the call to B. int h, i; void B(int* w) { int j, k; i = 2*(*w); *w = *w+1; } void A(int* x, int* y) { bool i, j; B(&h); } int main() { int a, b; h = 5; a = 3; b = 2; A(&a, &b); } 7

Pass by Value-Result and Result o Pass by value at the time of the call and/or copy the result back to the argument at the end of the call. n Value-result is often called copy-incopy-out. o Reference and value-result are the same, except when aliasing occurs. That is, for example when: n the same variable is passed for two different parameters. 8

Passing by reference Vs. by Value-result Void f (int &x, int &y) { x=x+1; y=y+1; } Call (a. b) Call (a, a) Procedure f (x, y: in out Integer) is begin x : = x+1; y : = y+1; end f; increment a and b increment a by 1 (Ada) increment a by 2 (C++) 9

Pass by Name o Textually substitute the argument for every instance of its corresponding parameter in the function body. n late binding, since evaluation of the argument is delayed until its occurrence in the function body is actually executed. n Example: C/C++ mechanism for #define o Implementation is dynamically for each call 10

Activation Record o A block of information associated with each function call, which includes: § § § § parameters and local variables Return address Saved registers Temporary variables Return value Static link - to the function’s static parent Dynamic link - to the activation record of the caller 11

Recursive Functions o A function that can call itself, either directly or indirectly, is a recursive function. E. g. , int factorial (int n) { if (n < 2) return 1; else return n*factorial(n-1); } 12

Run Time Stack o A stack of activation records. n Each new call pushes an activation record, and n each completing call pops the topmost one. n So, the topmost record is the most recent call, and the stack has all active calls at any run-time moment. q For example, consider the call factorial(3). § This places one activation record onto the stack and generates a second call factorial(2). This call generates the call factorial(1), so that the stack gains three activation records. 13

Stack Activity for the Call factorial(3) n 3 First call n 3 n 3 n 2 n 2 n 1 Second call Third call returns 1 n 3 Second call First call returns 2*1=2 returns 3*2=6 14
- Slides: 14