tailieunhanh - Lecture Programming languages (2/e): Chapter 9 - Tucker, Noonan

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 Basic Terminology Function Call and Return Parameters Parameter Passing Mechanisms Activation Records Recursive Functions Run Time Stack Basic Terminology Value-returning functions: known as “non-void functions/methods” in C/C++/Java called from within an expression. ., 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. ., strcpy(s1, s2); Function Call and Return Example C/C++ Program Fig 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); } Parameters Definitions An argument is an expression that appears in a function call. A parameter is an identifier that appears in a function declaration. ., in Figure 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. ., 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. ., the call A(y=>b, x=>a) is the same as A(a, b) 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. ., in the call A(a, b) in Fig. , 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 Basic Terminology Function Call and Return Parameters Parameter Passing Mechanisms Activation Records Recursive Functions Run Time Stack Basic Terminology Value-returning functions: known as “non-void functions/methods” in C/C++/Java called from within an expression. ., 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. ., strcpy(s1, s2); Function Call and Return Example C/C++ Program Fig 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); } Parameters Definitions An argument is an expression that appears in a

TỪ KHÓA LIÊN QUAN