chapter9subprograms内容摘要:

C – Passbyvalue 5. Pascal and Modula2 – Default is passbyvalue。 passbyreference is optional 6. C++ – Like C, but also allows reference type parameters, which provide the efficiency of passbyreference with inmode semantics 924 Parameter Passing Methods 7. Ada – All three semantic modes are available – If out, it cannot be referenced – If in, it cannot be assigned 8. Java – Like C++, except only references 925 Parameter Passing Methods • Type checking parameters (now considered very important for reliability) – FORTRAN 77 and original C: none – Pascal, FORTRAN 90, Java, and Ada: it is always required – ANSI C and C++: choice is made by the user double fun(x) int x。 {…} 926 Parameter Passing Methods • Implementing Parameter Passing – ALGOL 60 and most of its descendants use the runtime stack – Value copy it to the stack。 references are indirect to the stack – Result same – Reference regardless of form, put the address in the stack – Name runtime resident code segments or subprograms evaluate the address of the parameter。 called for each reference to the formal。 • 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。 the user must include the storage mapping function, which is in terms of the size parameters – P370 处理多维数组的方法 931 Parameter Passing Methods • Pascal – Not a problem (declared size is part of the array’s type) • Ada – Constrained arrays like Pascal – Unconstrained arrays declared size is part of the object declaration (Java is similar) 932 Parameter Passing Methods • Design Considerations for Parameter Passing 1. Efficiency 2. Oneway or twoway – These two are in conflict with one another! – Good programming = limited access to variables, which means oneway whenever possible – Efficiency = pass by reference is fastest way to pass structures of significant size – Also, functions should not allow reference parameters – 注意:大数组通常按引用传递; – P376~377 例,按值与结果传递时, i 的值是多少。 933 Parameters that are Subprogram Names • Issues: 1. Are parameter types checked? – Early Pascal and FORTRAN 77 do not – Later versions of Pascal and FORTRAN 90 do – Ada does not allow subprogram parameters – Java does not。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。