Đang chuẩn bị liên kết để tải về tài liệu:
Lecture Programming languages (2/e): Chapter 9 - Tucker, Noonan
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Functions represent the key element of procedural abstraction in any language. An understanding of the semantics of function definition and call is central to any study of programming languages. The implementation of functions also requires an understanding of the static and dynamic elements of memory, including the run-time stack. The stack also helps us understand other ideas like the scope of a name and the lifetime of an object. These topics are treated in Chapter 9. | Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis Contents 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 9.1 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); 9.2 Function Call and Return Example C/C++ Program Fig 9.1 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); } 9.3 Parameters Definitions An argument is an expression that appears in a function call. A parameter is an identifier that appears in a function declaration. 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. 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) 9.4 Parameter Passing Mechanisms By value By reference By value-result By result By name 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 | Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis Contents 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 9.1 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); 9.2 Function Call and Return Example C/C++ Program Fig 9.1 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); } 9.3 Parameters Definitions An argument is an expression that appears in a