Download presentation
Presentation is loading. Please wait.
1
ISBN 0-321-33025-0 Chapter 9 Subprograms CE2004 Principles of Programming Languages
2
1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprogram Names Overloaded Subprograms Generic Subprograms Design Issues for Functions User-Defined Overloaded Operators Coroutines
3
1-3 Short-Circuit Evaluation (Chapter 7)
4
1-4 Short-Circuit Evaluation A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators. –For example, the value of the arithmetic expression (3 * a) * (b / 13 - 1) is independent of the value of (b / 13 - l) if a is 0, because 0 * x = 0 for any x. So when a is 0, there is no need to evaluate (b / 13 - l) or perform the second multiplication. –However, in arithmetic expressions this shortcut is not easily detected during execution, so it is NEVER taken.
5
1-5 Another Example The value of the Boolean expression (a >= 0) && (b < 10) is independent of the second relational expression if a < 0, because (FALSE && x) is FALSE for all values of x. So when a < 0, there is no need to evaluate b, the constant 10, the second relational expression, or the && operation. Unlike the case of arithmetic expressions, this shortcut can be easily discovered during execution and taken.
6
1-6 A Potential Problem with Non-short-Circuit Evaluation of Boolean Expressions Suppose –Java did NOT do short-circuit evaluation. –suppose we write a table lookup loop using the while statement. –One simple version of Java code for such a lookup, assuming that list, which has listlen elements, is the array to be searched and key is the searched-for value, is index = 0; while ((index key)) Index = index +1 ; If evaluation is not short circuit, both relational expressions in the Boolean expression of the while statement are evaluated, regardless of the value of the first. Thus, if key is not in list, The iteration that has index == listlen will reference list[listlen], which causes the indexing error because list is declared to have listlen-l as an upper bound subscript value.
7
1-7 Short-circuit Evaluation Could Solve the Previous Problem If a language provides short-circuit valuation of Boolean expressions and it is used, situation in the previous slide is not a problem. In the preceding example, a short-circuit evaluation scheme would evaluate the first operand of the AND operator, but it would skip the second operand if the first operand is false.
8
1-8 Short-Circuit Evaluation May Result in Errors Short-circuit evaluation of expressions exposes the problem of allowing side effects in expressions. Suppose that short-circuit evaluation is used on an expression and part of the expression that contains a side effect is not evaluated; then the side effect will only occur in complete evaluations of the whole expression. If program correctness depends on the side effect, short-circuit evaluation can result in a serious error.
9
1-9 Example Considconsider the C expression (a > b) || (b++ / 3) In this expression, b is changed (in the second arithmetic expression) only when a<=b. If the programmer assumed b would be changed every time this expression is evaluated during execution, the program will fail.
10
1-10 Overloaded Operators Arithmetic operators are often used for more than one purpose. –For example, + is frequently used for integer addition and floating-point addition. –Some languages, Java for example, also use it for string catenation. The above multiple use of an operator is called operator overloading and is generally thought to be acceptable, as long as readability and/or reliability do not suffer.
11
1-11 Chapter 9
12
1-12 Abstraction Facilities in a Programming Language Two fundamental abstraction facilities can be included in a programming language: –process abstraction In the early history of high-level programming languages, only process abstraction was recognized and included. Process abstraction has been a central concept in all programming languages. –data abstraction In the 1980s, however, many people began to believe that data abstraction was equally important. Data abstraction is discussed in detail in Chapter 11.
13
1-13 Properties of Subprograms All subprograms discussed in this chapter, except the coroutines described in Section 9.11, have the following characteristics; –Each subprogram has a single entry point. –The calling program unit is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time. –Control always returns to the caller when the subprogram execution terminates.
14
1-14 Subprogram-Related Definitions A subprogram definition describes the interface to and the actions of the subprogram abstraction. A subprogram call is the explicit request that the subprogram be executed. A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution. The two fundamental kinds of subprograms, procedures and functions, are defined and discussed in Section 9.2.4.
15
1-15 Subprogram Headers A subprogram header, which is the first part of the definition, serves several purposes. –First, it specifies that the following syntactic unit is a subprogram definition of some particular kind. Some programming languages include two different kinds of subprograms; specifically, procedures and functions. –Second, it provides a name for the subprogram. –Third, it may optionally specify a list of parameters. They are optional because not all subprogram definitions have parameters.
16
1-16 Header Examples The following is the header of a Fortran subprogram named Adder. Subroutine Adder(parameters) In Ada, the header for Adder would be procedure Adder(parameters)
17
1-17 C Function Header No special word appears in the header of a C subprogram. C has only one kind of subprogram, the function, and the header of a function is recognized by context rather than by a special word. For example, void Adder(parameters) –would serve as the header of a function named adder, where void indicates that it does not return a value.
18
1-18 The Parameter Profile and the Protocol of a Subprogram The parameter profile (sometimes called the signature ) of a subprogram is the number, order, and types of its formal parameters. The protocol of a subprogram is its parameter profile plus, if it is a function, its return type.
19
1-19 The Declarations of Variables in C In C the declarations of variables can be used to provide type information but not to define variables. A variable declaration in C is used only outside function definitions. A declaration is necessary when a variable must be referenced before the compiler has seen its definition.
20
1-20 Subprogram Declarations Subprograms can have declarations as well as definitions. –This form parallels the variable declarations and definitions in C. Subprogram declarations provide the subprogram’s protocol, but do not include their bodies. –They are necessary when the compiler must translate a subprogram call before it has seen that subprogram’s definition. In both the cases of variables and subprograms, declarations are needed for static type checking. –In the case of subprograms, it is the type of the parameters that must be checked.
21
1-21 Subprogram Declarations in Different Languages Function declarations are common in C and C++ programs, where they are called prototypes. Java and C# do not need declarations of their methods, because there is no requirement in those languages that methods be defined before they are called.
22
1-22 Approaches a Subprogram Could Use to Access Data Subprograms typically describe computations. There are two ways that a subprogram can gain access to the data that it is to process: –through direct access to nonlocal variables (declared elsewhere but visible in the subprogram) –through parameter passing.
23
1-23 Comparisons between Nonlocal Variables and Parameters Data passed through parameters are accessed through names that are local to the subprogram. Parameter passing is more flexible than direct access to nonlocal variables. In essence, a subprogram with parameter access to the data that it is to process is a parameterized computation. –It can perform its computation on whatever data it receives through its parameters (presuming the types of the parameters are as expected by the subprogram).
24
1-24 Using a Subprogram as a Parameter In some situations, it is convenient to be able to transmit computations, rather than data, as parameters to subprograms. In these cases, the name of the subprogram that implements that computation may be used as a parameter. –This form of parameter is discussed in Section 9.6. –Data parameters are discussed in Section 9.5.
25
1-25 Formal Parameters The parameters in the subprogram header are called formal parameters. They are sometimes thought of as dummy variables because they are not variables in the usual sense: In some cases, they are bound to storage only when the subprogram is called.
26
1-26 Actual Parameters Subprogram call statements must include –the name of the subprogram –a list of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters. They must be distinguished from formal parameters because the two can have different restrictions on their forms, and of course their uses are quite different.
27
1-27 Positional Parameters In nearly all programming languages, the correspondence between actual and formal parameters—or the binding of actual parameters to formal parameters—is done by simple position : –The first actual parameter is bound to the first formal parameter and so forth. –Such parameters are called positional parameters. This is an effective and safe method for relatively short parameter lists.
28
1-28 Keyword Parameters When lists are long, however, it is easy for the programmer to make mistakes in the order of parameters in the list. One solution to this problem is to provide keyword parameters, in which the name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter.
29
1-29 Advantages of Keyword Parameters The advantage of keyword parameters is that they can appear in any order in the actual parameter list. Ada procedures can be called using this method, as in Sumer(Length => My_Length, List => My_Array, Sum => My_Sum); –where the definition of Sumer has the formal parameters Length, List, and Sum.
30
1-30 Disadvantages of Keyword Parameters The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters.
31
1-31 Combining Keyword Parameters with Positional Parameters In addition to keyword parameters, Ada and Fortran 90 allow positional parameters. The two can be mixed in a call, as in Sumer(Length, List => My_Array, Sum => My_Sum); The only restriction with this is that after a keyword parameter appears in the list, all remaining parameters must be keyworded. This is necessary because position may no longer be well defined after a keyword parameter has appeared.
32
1-32 When the Number of Actual Parameters Is Fewer than the Number of Formal Parameters In most languages that do not have default values for formal parameters, the number of actual parameters in a call must match the number of formal parameters in the subprogram definition header. However, in C, C++, Perl, and JavaScript this is not required. –When there are fewer actual parameters in a call than formal parameters in a function definition, it is the programmer's responsibility to ensure that the parameter correspondence, which is always positional, and the subprogram execution are sensible.
33
1-33 Advantages of Variable Number of Parameters Although this design, which allows a variable number of parameters, is clearly prone to error, it is also sometimes convenient. –For example, the printf function of C can print any number of items (data values and/or literal strings).
34
1-34 Disadvantages of Variable Number of Parameters Format string attacks. E.g. int i; printf(“%d %d”,i); don’t match
35
1-35 Procedures and Functions There are two distinct categories of subprograms: procedures and functions, both of which can be viewed as approaches to extending the language.
36
1-36 Procedures and Subroutines Procedures are collections of statements that define parameterized computations. These computations are enacted by single call statements. In effect, procedures define new statements. –For example, because Ada does not have a sort statement, a user can build a procedure to sort arrays of data and use a call to that procedure in place of the unavailable sort statement. In Ada, procedure are called that; in Fortran, they are called subroutines.
37
1-37 Send back Results to Calling Procedures Procedures can produce results in the calling program unit by two methods. –First, if there are variables that are not formal parameters but are still visible in both the procedure and the calling program unit, the procedure can change them. –Second, if the subprogram has formal parameters that allow the transfer of data to the caller, those parameters can be changed.
38
1-38 Functions Functions structurally resemble procedures but are semantically modeled on mathematical functions. If a function is a faithful model, it produces no side effects; that is, it modifies neither its parameters nor any variables defined outside the function. In practice, many functions in programs have side effects.
39
1-39 Function Invoking and Its Return Value Functions are called by appearances of their names in expressions, along with the required actual parameters. The value produced by a function's execution is returned to the calling code, effectively replacing the call itself. –For example, the value of the expression f(x) is whatever value f produces when called with the parameter x. –For a function that does not produce side effects, the returned value is its only effect.
40
1-40 Functions Define New User-defined Operators For example, if a language does not have an exponentiation operator, a function can be written that returns the value of one of its parameters raised to the power of another parameter. Its header in C++ could be float power (float base, float exp) which could be called with result = 3.4 * power(10.0, x)
41
1-41 Overloaded Subprograms and Generic Subprograms An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment. A generic subprogram is one whose computation can be done on data of different types with different calls.
42
1-42 Semantics Models of Formal Parameters Formal parameters are characterized by one of three distinct semantics models : 1.they can receive data from the corresponding actual parameter; 2.they can transmit data to the actual parameter; 3.they can do both. These three semantics models are called –in mode –out mode –inout mode
43
1-43 Figure 9.1
44
1-44 Conceptual Models of How Data Transfers Take Place There are two conceptual models of how data transfers take place in parameter transmission; –either an actual value is physically copied (to the caller, to the callee, or both ways) –an access path is transmitted Most commonly, the access path is a simple pointer.
45
1-45 Implementation Models of Parameter Passing Pass-by-Value Pass-by-Result Pass-by-Value-Result Pass-by-Reference Pass-by-Name
46
1-46 Pass-by-Value When a parameter is passed by value, the value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable in the subprogram, thus implementing in-mode semantics.
47
1-47 Approaches to Implement Pass-by-Value Pass-by-value is normally implemented by copy, because accesses are usually more efficient with this method. It could be implemented by transmitting an access path to the value of the actual parameter in the caller, but that would require that the value be in a write-protected cell (one that can only be read). –Enforcing the write protection is not always a simple matter. For example, suppose the subprogram to which the parameter was passed passes it in turn to another subprogram.
48
1-48 Disadvantages of the Pass-by-Value Method The main disadvantage of the pass-by-value method if copies are used is that additional storage is required for the formal parameter, either –in the called subprogram –in some area outside both the caller and the called subprogram In addition, the actual parameter must be copied to the storage area for the corresponding formal parameter. The storage and the copy operations can be costly if the parameter is large, such as an array with many elements.
49
1-49 Pass-by-Result Pass-by-result is an implementation model for out-mode parameters. When a parameter is passed by result, NO value is transmitted to the subprogram. The corresponding formal parameter acts as a local variable, but just before control is transferred back to the caller, its value is transmitted back to the caller's actual parameter, which obviously must be a variable,
50
1-50 Price to Pay for Different Implementations If values are returned by copy (as opposed to access paths), as they typically are, pass-by-result also requires the extra storage and the copy operations that are required by pass-by-value. As with pass-by-value, the difficulty of implementing pass-by-result by transmitting an access path usually results in it being implemented by data copy. –In this case, the problem is in ensuring that the initial value of the actual parameter is not used in the called subprogram.
51
1-51 Problem One problem with the pass-by-result model is that there can be an actual parameter collision, such as the one created with the call sub(p1,p1) –In sub, assuming the two formal parameters have different names, the two can obviously be assigned different values. –Then whichever of the two is assigned to their corresponding actual parameter last becomes the value of p1. Thus the order in which the actual parameters are assigned determines their value. –Because the order is usually implementation dependent, portability problems can occur that are difficult to diagnose.
52
1-52 Pass-by-Value-Result Pass-by-value-result is an implementation model for inout-mode parameters in which actual values are copied. It is in effect a combination of pass-by-value and pass-by-result. –The value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable. In fact, pass-by-value-result formal parameters must have local storage associated with the called subprogram. –At subprogram termination, the value of the formal parameter is transmitted back to the actual parameter.
53
1-53 Alias of Pass-by-Value-Result Pass-by-value-result is sometimes called pass-by-copy because the actual parameter is copied to the formal parameter at subprogram entry and then copied back at subprogram termination.
54
1-54 Disadvantages and Problems of Pass-by-Value-Result Pass-by-value-result shares with pass-by- value and pass-by-result the disadvantages of requiring multiple storage for parameters and time for copying values. It shares with pass-by-result the problems associated with the order in which actual parameters are assigned.
55
1-55 Pass-by-Reference Pass-by-reference is a second implementation model for inout-mode parameters. Rather than copying data values back and forth, however, the pass-by-reference method transmits an access path, usually just an address, to the called subprogram. This provides the access path to the cell storing the actual parameter. Thus the called subprogram is allowed to access the actual parameter in the calling program unit. In effect, the actual parameter is shared with the called subprogram.
56
1-56 Advantages The advantage of pass-by-reference is that the passing process itself is efficient, in terms of both time and space. Duplicate space is not required, nor is any copying required.
57
1-57 Disadvantages – (1) There are, however, several disadvantages to the pass-by-reference method. –First, access to the formal parameters will be slower than pass-by-value parameters, because of the additional level of indirect addressing that is required. –Second, if only one-way communication to the called subprogram is required, inadvertent and erroneous changes may be made to the actual parameter.
58
1-58 Disadvantages – (2) Another serious problem of pass-by-reference is that aliases can be created. This should be expected because pass-by-reference makes access paths available to the called subprograms, thereby broadening their access to nonlocal variables.
59
1-59 Aliases When parameters are passed by reference, collisions can occur between actual parameters. Consider a C function procedure that has two parameters that are to be passed by reference, as in void fun(int *first, int *aecond) If the call to fun happens to pass the same variable twice, as in fun(&total, &total) then first and second in fun will be aliases.
60
1-60 Pass-by-Name Pass-by-name is an inout-mode parameter transmission method that does not correspond to a single implementation model, as explained below. When parameters are passed by name, the actual parameter is, in effect, textually substituted for the corresponding formal parameter in all its occurrences in the subprogram.
61
1-61 Differences between Pass-by-Name and Other Approaches Pass-by-name is quite different from the methods discussed thus far. –In those cases, formal parameters are bound to actual values or addresses at the time of the subprogram call. –A pass-by-name formal parameter is bound to an access method at the time of the subprogram call, but the actual binding to a value or an address is delayed until the formal parameter is assigned or referenced.
62
1-62 Resulting Semantics If actual is a scalar variable, it is pass-by- reference. If actual is a constant expression, it is pass-by-value.
63
1-63 An Example in Algol [1]1 For example, to double the value of C[j], you can pass its name (not its value) into the following procedure. procedure double(x); real x; begin x := x * 2 end; In general, the effect of pass-by-name is to textually substitute the argument expressions in a procedure call for the corresponding parameters in the body of the procedure, e.g., double(C[j]) is interpreted as C[j] := C[j] * 2.
64
1-64 Flexibility The objective of the late binding in pass- by-name parameters is flexibility. This is consistent with other situations in which we have encountered differences in binding time.
65
1-65 Run-Time Stacks and Parameter Passing In most contemporary languages, parameter communication takes place through the run-time stack. The run-time stack is initialized and maintained by the run-time system, which is a system program that manages the execution of programs. (Not true for C) The run-time stack is used extensively for subprogram control linkage and parameter passing, as discussed in Chapter 10.
66
1-66 Assumption In the following discussion, the author assumes that the stack is used for all parameter transmission.
67
1-67 Handling Pass-by-Value Parameters Pass-by-value parameters have their values copied into stack locations. The stack locations then serve as storage for the corresponding formal parameters. C uses pass-by-value –Pass-by-reference (inout mode) semantics is achieved by using pointers as parameters.
68
1-68 Handling Pass-by-Result Parameters Pass-by-result parameters are implemented as the opposite of pass-by- value. The values assigned to the pass-by-result actual parameters are placed in the stack, where they can be retrieved by the calling program unit upon termination of the called subprogram.
69
1-69 Handling Pass-by-Value-Result Parameters Pass-by-value-result parameters can be implemented directly from their semantics as a combination of pass-by-value and pass-by-result. The stack location for the parameters is initialized by the call and is then used like a local variable in the called subprogram.
70
1-70 Handling Pass-by-Reference Parameters Pass-by-reference parameters are perhaps the simplest to implement. Regardless of the type of the actual parameter, only its address must be placed in the stack.
71
1-71 Chapter 10
72
1-72 Subprogram Linkage The subprogram call and return operations of a language are together called its subprogram linkage.
73
1-73 Activation Record The format, or layout, of the noncode part of a subprogram is called an activation record, because the data it describes are only relevant during the activation of the subprogram. The form of an activation record is static. An activation record instance is a concrete example of an activation record, a collection of data in the form of an activation record.
74
1-74 Return Addresses The return address often consists of a pointer to the code segment of the caller and an offset address in that code segment of the instruction following the call.
75
1-75 Dynamic Link The dynamic link is a pointer to the the activation record instance of the caller.
76
1-76 Figure 10.3 The above figure is described in the text book. However, the layout of the activation record of a real implementation may be different than it (see next slide for more details).
77
1-77 Layout of an Activation Record of a Real Implementation Parameters Return address Dynamic link Local variables
78
1-78 A Skeletal C Program void fun1(int x){ int y;... <----------------------------------2 fun3(y);... } void fun2(float r){ int s,t;... <----------------------------------1 fun1(s);... } void fun3(int q){... <----------------------------------3 } void main(){ float p;... fun2(p);... }
79
1-79 The Sequence of Procedure Calls The sequence of procedure calls in the program in the previous slide is main calls fun2 fun2 calls fun1 fun1 calls fun3
80
1-80 Figure 10.5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.