UT Dallas CS 4337 - #Sebesta ch09 subprogram (65 pages)

Previewing pages 1, 2, 3, 4, 30, 31, 32, 33, 34, 62, 63, 64, 65 of 65 page document View the full content.
View Full Document

#Sebesta ch09 subprogram



Previewing pages 1, 2, 3, 4, 30, 31, 32, 33, 34, 62, 63, 64, 65 of actual document.

View the full content.
View Full Document
View Full Document

#Sebesta ch09 subprogram

37 views


Pages:
65
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Chapter 9 Subprograms Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter Passing Methods Parameters That Are Subprograms Calling Subprograms Indirectly Overloaded Subprograms Generic Subprograms Design Issues for Functions User Defined Overloaded Operators Closures Coroutines Copyright 2012 Addison Wesley All rights reserved 1 2 Introduction Two fundamental abstraction facilities Process abstraction Emphasized from early days Discussed in this chapter Data abstraction Emphasized in the1980s Discussed at length in Chapter 11 Copyright 2012 Addison Wesley All rights reserved 1 3 Fundamentals 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 terminates Copyright 2012 Addison Wesley All rights reserved 1 4 Basic Definitions A subprogram definition describes the interface to and the actions of the subprogram abstraction In Python function definitions are executable in all other languages they are non executable In Ruby function definitions can appear either in or outside of class definitions If outside they are methods of Object They can be called without an object like a function In Lua all functions are anonymous A subprogram call is an explicit request that the subprogram be executed Copyright 2012 Addison Wesley All rights reserved 1 5 Basic Definitions A subprogram header is 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 protocol is a subprogram s parameter profile and if it is a function its return type Copyright 2012 Addison Wesley All rights reserved 1 6 Basic Definitions continued Function declarations in C and C are often called prototypes 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 Copyright 2012 Addison Wesley All rights reserved 1 7 Actual 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 Advantage Parameters can appear in any order thereby avoiding parameter correspondence errors Disadvantage User must know the formal parameter s names Copyright 2012 Addison Wesley All rights reserved 1 8 Formal Parameter Default Values In certain languages e g C Python Ruby Ada PHP formal parameters can have default values if no actual parameter is passed In C default parameters must appear last because parameters are positionally associated no keyword parameters Variable numbers of parameters C methods can accept a variable number of parameters as long as they are of the same type the corresponding formal parameter is an array preceded by params In Ruby the actual parameters are sent as elements of a hash literal and the corresponding formal parameter is preceded by an asterisk In Python the actual is a list of values and the corresponding formal parameter is a name with an asterisk In Lua a variable number of parameters is represented as a formal parameter with three periods they are accessed with a for statement or with a multiple assignment from the three periods Copyright 2012 Addison Wesley All rights reserved 1 9 Procedures and Functions There are two categories of subprograms Procedures are collection of statements that define parameterized computations Functions structurally resemble procedures but are semantically modeled on mathematical functions They are expected to produce no side effects In practice program functions have side effects Copyright 2012 Addison Wesley All rights reserved 1 10 Design Issues for Subprograms Are local variables static or dynamic Can subprogram definitions appear in other subprogram definitions What parameter passing methods are provided Are parameter types checked If subprograms can be passed as parameters and subprograms can be nested what is the referencing environment of a passed subprogram Can subprograms be overloaded Can subprogram be generic If the language allows nested subprograms are closures supported Copyright 2012 Addison Wesley All rights reserved 1 11 Local Referencing Environments Local variables can be stack dynamic 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 Advantages and disadvantages are the opposite of those for stack dynamic local variables Copyright 2012 Addison Wesley All rights reserved 1 12 Semantic Models of Parameter Passing In mode Out mode Inout mode Copyright 2012 Addison Wesley All rights reserved 1 13 Models of Parameter Passing Copyright 2012 Addison Wesley All rights reserved 1 14 Conceptual Models of Transfer Physically move a value Call by Value Move an access path to a value Call by Reference Copyright 2012 Addison Wesley All rights reserved 1 15 Pass 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 Disadvantages if by physical move additional storage is required stored twice and the actual move can be costly for large parameters Disadvantages if by access path method must writeprotect in the called subprogram and accesses cost more indirect addressing Copyright 2012 Addison Wesley All rights reserved 1 16 Pass 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 by physical move Require extra storage location and copy operation Potential problems whichever formal parameter is copied back will represent the current value of p1 sub list sub sub


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view #Sebesta ch09 subprogram 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 #Sebesta ch09 subprogram 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?