ch09 - Programming Languages 2nd edition Tucker and Noonan...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 2
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);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9.2 Function Call and Return Fig 9.1 : Example C/C++ Program 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() {
Background image of page 4
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
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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: Python: parameters aren’t typed Perl - parameters aren’t declared in a function header. Instead, parameters are stored in an array @_ , and are accessed using an array index. 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) .
Background image of page 6
9.4 Parameter Passing Mechanisms By value By reference By value-result By result By name
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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.
Background image of page 8
Pass by Value Pass by value doesn’t allow the called function to modify an argument’s value in the caller’s environment. All arguments in C and Java are passed by value. But references can be passed to allow argument values to be modified.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Simulating Pass by Reference in C Compute the address of the argument at the time of the call and assign it to the parameter. 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;
Background image of page 10
Pass by reference means the memory address of the argument is copied to the corresponding parameter so the parameter is an indirect reference (a pointer) to the actual argument. Assignments to the parameter affect the
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 40

ch09 - Programming Languages 2nd edition Tucker and Noonan...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online