220 likes | 293 Views
Programming Languages Tucker and Noonan. Chapter 9 : Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters 9.4 Parameter Passing Mechanisms 9.5 Activation Records 9.6 Recursive Functions 9.7 Run Time Stack. Basic Terminology. Value-returning functions:
E N D
Programming LanguagesTucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters 9.4 Parameter Passing Mechanisms 9.5 Activation Records 9.6 Recursive Functions 9.7 Run Time Stack CSC321: Programming Languages
Basic Terminology • Value-returning functions: • known as “non-void functions/methods” in C/C++/Java • called from within an expression. e.g., x = (b*b - sqrt(4*a*c))/2*a • Non-value-returning functions: • known as “procedures” in Ada, “subroutines” in Fortran, “void functions/methods” in C/C++/Java • called from a separate statement. • e.g., strcpy(s1, s2); CSC321: Programming Languages
Function Call and Return 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); } • Example C/C++ • Program Fig 9.1 CSC321: Programming Languages
Parameters • Definitions • An argument is an expression that appears in a function call. (Actual parameter) • A parameter is an identifier that appears in a function declaration. (Formal parameter) • E.g., in Figure 9.1 • The call A(a, b) has arguments a and b. • The function declaration A has parameters x and y. CSC321: Programming Languages
Parameter-Argument Matching • Usually by number and by position. I.e., any call to A must have two arguments, and they must match the corresponding parameters’ types. • Exceptions: • Perl - parameters aren’t declared in a function header. Instead, parameters are available in an array @_, and are accessed using a subscript on this array. • Ada - arguments and parameters can be linked by name. E.g., the call A(y=>b, x=>a) is the same as A(a, b) CSC321: Programming Languages
Parameter Passing Mechanisms • Pass by value • Pass by reference • Pass by value-result • Pass by result • Pass by name CSC321: Programming Languages
Procedure Forward Reference • The procedure declaration and the body definition can appear separately (in different modules/blocks) • Forward declaration: Most languages declare procedures/functions before call procedures/functions • Declare anywhere (Algol, Java): the order of procedure declaration and calls does not matter CSC321: Programming Languages
Pass by Value • Compute the value of the argument at the time of the call and assign that value to the parameter. • E.g., in the call A(a, b) in Fig. 9.1, a and b are passed by value. So the values of parameters x and y become 3 and 2, respectively when the call begins. • So passing by value doesn’t normally allow the called function to modify an argument’s value. • All arguments in Java are passed by value. • But references can be passed to allow argument values to be modified. E.g., void swap(int *a, int *b) { … } CSC321: Programming Languages
Pass by Reference 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); } • Compute the address of the argument at the time of the call and assign it to the parameter. Example Fig 9.3 • Since h is passed by reference, its value changes during the call to B. CSC321: Programming Languages
Pass by Value-Result and Result • Pass by value at the time of the call and/or copy the result back to the argument at the end of the call. • E.g., Ada’s in out parameter can be implemented as value-result. • Value-result is often called copy-in-copy-out. • Reference and value-result are the same, except when aliasing occurs. That is, when: • the same variable is both passed and globally referenced from the called function, or • the same variable is passed for two different parameters. CSC321: Programming Languages
Pass by Name • Textually substitute the argument for every instance of its corresponding parameter in the function body. • Originated with Algol 60 (Jensen’s device), but was dropped by Algol’s successors -- Pascal, Ada, Modula. • Exemplifies late binding, since evaluation of the argument is delayed until its occurrence in the function body is actually executed. • Associated with lazy evaluation in functional languages (see, e.g., Haskell discussion in Chapter 14). CSC321: Programming Languages
Activation Records • 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 CSC321: Programming Languages
Recursive Functions • 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); } self-call CSC321: Programming Languages
Run Time Stack • A stack of activation records. • Each new call pushes an activation record, and each completing call pops the topmost one. • So, the topmost record is the most recent call, and the stack has all active calls at any run-time moment. • 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. CSC321: Programming Languages
Stack Activity for the Call factorial(3) Fig. 9.7 n 3 n 3 n 3 n 3 n 3 n 2 n 2 n 2 n 1 Second call returns 2*1=2 First call returns 3*2=6 Third call returns 1 First call Second call CSC321: Programming Languages
h 5 i undef h 5 i10 h undef i undef a 3 b2 a 3 b2 a 3 b2 x 3 y2 i undef j undef x 3 y2 i undef j undef w 5 j undef k undef Stack Activity for Program in Fig. 9.1 • Fig. 9.8 (links not shown) Activation of main main calls A A calls B CSC321: Programming Languages
Parameter Replacement • Positions in the list • E.g. (C language) • Declare: int square(int x, int y) { return x*y;} • Call: square(5, 8) • Formal parameters: int x and int y • Actual parameters: 5 and 8 • Replacement as the positions: x 5, and y 8 CSC321: Programming Languages
Default Parameters (I) • C++, Ada • Procedure call may omit some actual parameters, if these parameters have default values • Procedure declaration must specify the default values, if any. E.g. void proc_name(int p1, int p2=0, int p3=1) { …} which means, if the actual parameter corresponding to p2 is defaulted to 0, while the default value of the actual parameter corresponding to p3 is 1. Thus, procedure call proc_name(1) is equivalent to the procedure call proc_name(1,0,1) CSC321: Programming Languages
Default Parameters (II) • In the declaration, the parameters with default values must be at the end of the parameter-list. Why? Consider an example, if we declare the first parameter with default value: int my_proc(int p1=0, int p2, int p3=1) {…} Then the procedure call: my_proc(0,1) causes confusion: does it mean the call my_proc(0,0,1) (default p1) or the call my_proc(0,1,1) (default p3) ? • Even put default values at the end of parameter list, still exist confusions. Consider the declaration: void my_name(int p1, int p2=1, int p3=1) {…} and the call: my_name(1,1). Does it mean whether p2 or p3 is default? • To avoid this confusion, C++, Ada, Java use the rule: replace the formal parameters from left to right. Thus, the above call uses default p3. CSC321: Programming Languages
Arbitrary Number of Parameters • Lisp/Scheme • Declaration do not specify a parameter number as the number of parameters, and the number of actual parameters can be arbitrary. • E.g. (list arg1 arg2 … argn) parameters are: arg1 arg2 .. argn CSC321: Programming Languages
Named Parameters • PL/I, Fortran, Ada • At call, specify the values with the named formal parameters. E.g. my_proc(p2=2); my_proc(p1=1,p2=2,p3=3); • Advantage: clear • Disadvantage: remember the formal parameters’ name CSC321: Programming Languages
Mixed Replacement • Ada • First in terms of positions, and then follow the named parameters • E.g. • Declaration: proc(int p1, double p2, float p3, boolean p4) • Call: proc(1, 2.0, p4=>true, p3=>3.2) CSC321: Programming Languages