【正文】
91 Chapter 9 Subprograms Topics ? Fundamentals of Subprograms ? Design Issues for Subprograms ? Local Referencing Environments ? ParameterPassing Methods ? Parameters that are Subprogram Names ? Overloaded Subprograms ? Generic Subprograms ? Design Issues for Functions ? UserDefined Overloaded Operators ? Coroutines 92 Introduction ? Two fundamental abstraction facilities – Process abstraction (this chapter) – Data abstraction (Chapter 11) 93 Fundamentals of Subprograms ? General characteristics of subprograms: 1. A subprogram has a single entry point 2. The caller is suspended during execution of the called subprogram 3. Control always returns to the caller when the called subprogram’s execution terminates 94 Fundamentals of Subprograms ? Basic definitions: – A subprogram call is an explicit request that the subprogram be executed – A subprogram header is the first line of the definition, including the name, the kind of subprogram, and the formal parameters – The parameter profile of a subprogram is the number, order, and types of its parameters 95 Fundamentals of Subprograms ? Basic definitions (continued): – The protocol of a subprogram is its parameter profile plus, if it is a function, its return type – A subprogram declaration provides the protocol, but not the body, of the subprogram – A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram – An actual parameter represents a value or address used in the subprogram call statement 96 Fundamentals of Subprograms ? Actual/Formal Parameter Correspondence: 1. Positional 2. Keyword – . SORT(LIST = A, LENGTH = N)。 – Advantage: order is irrelevant – Disadvantage: user must know the formal parameter’s names – Default Values: . procedure SORT(LIST : LIST_TYPE。 LENGTH : INTEGER := 100)。 ... SORT(LIST = A)。 97 Fundamentals of Subprograms ? Procedures provide userdefined statements ? Functions provide userdefined operators 98 Design Issues for Subprograms 1. What parameter passing methods are provided? 2. Are parameter types checked?( actual Ps against with the formal Ps) 3. Are local variables static or dynamic? 4. Can subprogram definitions appear in other subprogram definitions? 5. What is the referencing environment of a passed subprogram? 6. Can subprograms be overloaded? 7. Are subprograms allowed to be generic? 8. Is either separate or independent pilation possible? 99 Local referencing environments ? If local variables are stackdynamic: – Advantages: a. Support for recursion b. Storage for locals is shared among some subprograms – Disadvantages: a. Allocation/deallocation time b. Indirect addressing c. Subprograms cannot be history sensitive ? Static locals are the opposite 910 Local referencing environments ? Language Examples: 1. FORTRAN 77 and 90 most are static, but the implementor can choose either (User can force static with SAVE) 2. C both (variables declared to be static are) (default is stack dynamic) 3. Pascal, Java, and Ada dynamic only 911 Parameter Passing Methods ? We discuss these at several different levels: – Semantic models: in mode, out mode, inout mode – Conceptual model