【正文】
? Very expensive, pared to reference or valueresult 927 Stack Implementation of ParameterPassing 928 Parameter Passing Methods ? Ada – Simple variables are passed by copy (valueresult) – Structured types can be either by copy or reference – This can be a problem, because a) Of aliases (reference allows aliases, but valueresult does not) b) Procedure termination by error can produce different actual parameter results 929 Parameter Passing Methods ? Multidimensional Arrays as Parameters – If a multidimensional array is passed to a subprogram and the subprogram is separately piled, the piler needs to know the declared size of that array to build the storage mapping function 930 Parameter Passing Methods ? C and C++ – Programmer is required to include the declared sizes of all but the first subscript in the actual parameter – This disallows writing flexible subprograms – Solution: pass a pointer to the array and the sizes of the dimensions as other parameters。 procedure sub2。 var x : integer。 procedure sub4 ( subx )。 end。 Parameters that are Subprogram Names What is the referencing environment of sub2 when it is called in sub4? – Shallow binding = sub2, sub4, sub3, sub1 x=4 – Deep binding = sub2,sub1 x=1 – Ad hoc binding = sub2,sub3 x=3 Never used 937 Overloaded Subprograms ? Def: An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment ? C++ and Ada have overloaded subprograms builtin, and users can write their own overloaded subprograms 938 Generic Subprograms ? A generic or polymorphic多態(tài)的 subprogram is one that takes parameters of different types on different activations ? Overloaded subprograms provide ad hoc polymorphism ? A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism 939 Generic Subprograms ? Examples of parametric polymorphism 1. C++ – Templated functions . template class Type Type max(Type first, Type second) { return first second ? first : second。 for (top = 0。bottom++) { if (list[top] list[bottom]) { temp = list [top]。 ... generic_sort(flt_list, 100)。 Independent Compilation ? Language Examples: – FORTRAN II to FORTRAN 77 independent – FORTRAN 90, Ada, C++, Java separate – Pascal allows neither 945 ?Design Issues for Functions 1. Are side effects allowed?副作用 a. Twoway parameters (Ada does not allow) b. Nonlocal reference (all allow) 2. What types of return values are allowed? 946 Design Issues for Functions ? Language Examples (for possible return types): 1. FORTRAN, Pascal only simple types 2. C any type except functions and arrays 3. Ada any type (but subprograms are not types) 4. C++ and Java like C, but also allow classes to be returned 947 Accessing Nonlocal Environments ? Def: The nonlocal variables of a subprogram are those that are visible but not declared in the subprogram ? Def: Global variables are those that may be visible i