Unformatted text preview:

ISBN 0-321-33025-0Chapter 9Chapter 9Chapter 9Chapter 9SubprogramsCopyright © 2006 Addison-Wesley. All rights reserved. 1-2Chapter 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• CoroutinesCopyright © 2006 Addison-Wesley. All rights reserved. 1-3Introduction• Two fundamental abstraction facilities– Process abstraction • Emphasized from early days– Data abstraction• Emphasized in the1980sCopyright © 2006 Addison-Wesley. All rights reserved. 1-4Fundamentals of Subprograms• Each subprogram has a single entry point• The calling program is suspended during execution of the called subprogram• Control always returns to the caller when the called subprogram’s execution terminatesCopyright © 2006 Addison-Wesley. All rights reserved. 1-5Basic Definitions• A subprogram definitiondescribes the interface to and the actions of the subprogram abstraction• A subprogram callis an explicit request that the subprogram be executed• A subprogram headeris the first part of the definition, including the name, the kind of subprogram, and the formal parameters• The parameter profile(aka signature) of a subprogram is the number, order, and types of its parameters• The protocolis a subprogram’s parameter profile and, if it is a function, its return typeCopyright © 2006 Addison-Wesley. All rights reserved. 1-6Basic Definitions (continued)• Function declarations in C and C++ are often called prototypes• A subprogram declarationprovides the protocol, but not the body, of the subprogram• A formal parameteris a dummy variable listed in the subprogram header and used in the subprogram• An actual parameterrepresents a value or address used in the subprogram call statementCopyright © 2006 Addison-Wesley. All rights reserved. 1-7Actual/Formal Parameter Correspondence• Positional– The binding of actual parameters to formal parameters is by position: the first actual parameter is bound to the first formal parameter and so forth– Safe and effective• Keyword– The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter– Parameters can appear in any orderCopyright © 2006 Addison-Wesley. All rights reserved. 1-8Formal Parameter Default Values• In certain languages (e.g., C++, Ada), formal parameters can have default values (if not actual parameter is passed)– In C++, default parameters must appear last because parameters are positionally associated• C# methods can accept a variable number of parameters as long as they are of the same typeCopyright © 2006 Addison-Wesley. All rights reserved. 1-9Procedures and Functions • There are two categories of subprograms–Proceduresare collection of statements that define parameterized computations–Functionsstructurally resemble procedures but are semantically modeled on mathematical functions• They are expected to produce no side effects• In practice, program functions have side effectsCopyright © 2006 Addison-Wesley. All rights reserved. 1-10Design Issues for Subprograms• What parameter passing methods are provided?• Are parameter types checked?• Are local variables static or dynamic?• Can subprogram definitions appear in other subprogram definitions?• Can subprograms be overloaded?• Can subprogram be generic?Copyright © 2006 Addison-Wesley. All rights reserved. 1-11Local Referencing Environments• Local variables can be stack-dynamic (bound to storage)– Advantages• Support for recursion• Storage for locals is shared among some subprograms– Disadvantages• Allocation/de-allocation, initialization time• Indirect addressing• Subprograms cannot be history sensitive• Local variables can be static– More efficient (no indirection)– No run-time overhead– Cannot support recursionCopyright © 2006 Addison-Wesley. All rights reserved. 1-12Parameter Passing Methods• Ways in which parameters are transmitted to and/or from called subprograms– Pass-by-value– Pass-by-result– Pass-by-value-result– Pass-by-reference– Pass-by-nameCopyright © 2006 Addison-Wesley. All rights reserved. 1-13Models of Parameter PassingCopyright © 2006 Addison-Wesley. All rights reserved. 1-14Pass-by-Value (In Mode)• The value of the actual parameter is used to initialize the corresponding formal parameter– Normally implemented by copying– Can be implemented by transmitting an access path but not recommended (enforcing write protection is not easy)– When copies are used, additional storage is required– Storage and copy operations can be costlyCopyright © 2006 Addison-Wesley. All rights reserved. 1-15Pass-by-Result (Out Mode)• When a parameter is passed by result, no value is transmitted to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller– Require extra storage location and copy operation• Potential problem: sub(p1, p1); whichever formal parameter is copied back will represent the current value of p1Copyright © 2006 Addison-Wesley. All rights reserved. 1-16Pass-by-Value-Result (inout Mode)• A combination of pass-by-value and pass-by-result• Sometimes called pass-by-copy• Formal parameters have local storage• Disadvantages:– Those of pass-by-result– Those of pass-by-valueCopyright © 2006 Addison-Wesley. All rights reserved. 1-17Pass-by-Reference (Inout Mode)• Pass an access path• Also called pass-by-sharing• Passing process is efficient (no copying and no duplicated storage)• Disadvantages– Slower accesses (compared to pass-by-value) to formal parameters– Potentials for un-wanted side effects– Un-wanted aliases (access broadened)Copyright © 2006 Addison-Wesley. All rights reserved. 1-18Pass-by-Name (Inout Mode)• By textual substitution• Formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment• Allows flexibility in late bindingCopyright © 2006 Addison-Wesley. All rights reserved. 1-19Implementing Parameter-Passing Methods• In most language parameter communication takes place thru the run-time stack•


View Full Document

DePaul CSC 447 - Subprograms

Download Subprograms
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Subprograms and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Subprograms 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?