Chapter 9

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What are the fundamentals of subprograms?

1. Each subprogram has a single entry point 2. The calling program is suspended during execution of the called subprogram 3. Control always returns to the caller when the called subprogram's execution terminates

Differences between generics in Java 5.0 and those of C++ and Ada:

1. Generic parameters in Java 5.0 must be classes 2. Java 5.0 generic methods are instantiated just once as truly generic methods - operates on the base Object class 3. Restrictions can be specified on the classes that can be passed to the generic method as generic parameters 4. Wildcard types of generic parameters - Useful for collection classes

What are the three modes of formal parameter passing?

1. In mode: Data is received from the actual parameter 2. Out mode: Data is returned to the actual parameter 3. Inout mode: Data is transferred in both directions

Two types of Parameter Correspondence

1. Positional 2. Keyword

What are the two fundamental abstraction facilities in programming languages?

1. Process Abstraction 2. Data Abstraction

Types of referencing environments

1. Shallow binding: The environment of the call statement that enacts the passed subprogram - Most natural for dynamic-scoped languages 2. Deep binding: The environment of the definition of the passed subprogram - Most natural for static-scoped languages 3. Ad hoc binding: The environment of the call statement that passed the subprogram

How do we call subprograms indirectly?

>Usually when there are several possible subprograms to be called and the correct one on a particular run of the program is not know until execution (e.g., event handling and GUIs) >Necessary to support polymorphism in OOP, where actual method invoked is not known until runtime and the real type of the object is determined >In C and C++, such calls are made through function pointers >Supporting polymorphism in a language is not free

Pass-by-Value-Result (Inout Mode)

A combination of pass-by-value and pass-by-result > Value is copied in at start and copied back out at end > Sometimes called pass-by-copy > Formal parameters have local storage Advantages: > Local access • Disadvantages: > Those of pass-by-result > Those of pass-by-value

Const references

A const reference is a reference where the original data cannot be modified from inside the function.

Chapter 9 Summary

A subprogram definition describes the actions represented by the subprogram • Subprograms can be either functions or procedures • Local variables in subprograms can be stackdynamic or static • Three models of parameter passing: in mode, out mode, and inout mode • Some languages allow operator overloading • Subprograms can be generic • A closure is a subprogram and its ref. environment

How is a multidimensional arrays passed as a parameters in ada?

Ada - not a problem - Constrained arrays - size is part of the array's type - Unconstrained arrays - declared size is part of the object declaration and can be retrieved at runtime via array attributes (the range attribute)

Advantages of Local Variables Being Static

Advantages and disadvantages are the opposite of those for stack-dynamic local variables

Advantages of Local Variables being Stack-Dynamic

Advantages: • Support for recursion • Storage for locals is shared among some subprograms Disadvantages: • Allocation/de-allocation, initialization time for each call • Indirect addressing • Subprograms cannot be history sensitive

Design Issues for Subprograms

Are local variables static or dynamic? • Can subprogram definitions appear in other subprogram definitions (i.e. nested procedures)? • 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?

Design Issues for Functions

Are side effects allowed? - Parameters should always be in-mode to reduce side effect (like Ada) What types of return values are allowed? - Most imperative languages restrict the return types - C allows any return type except arrays and functions - C++ is like C but also allows user-defined types - Ada subprograms can return any type (but Ada subprograms are not types, so they cannot be returned) - Java and C# methods can return any type (but because methods are not types, they cannot be returned) - Python and Ruby treat methods as first-class objects, so they can be returned, as well as any other class

Call-by-name (not in text)

Arguments to functions are not evaluated at all - Rather a function (thunk/closure) is passed - Whenever the parameter is used, the function is evaluated to produce a value - Function is re-evaluated at each reference - If the parameter is never used, the function is never evaluated • Similar to pass-by-name, except the local environment is captured in the thunk/closure

Pass-by-Name (Inout Mode)

By textual substitution • The actual parameter is effectively textually substituted for the formal parameter in all occurrences in the subprogram - Actual implementation is via special access methods • Corresponds to macro expansion in assembly languages

Parameter Passing Methods of Major Languages -- C -- Need to know -- C++ -- Need to know -- Java -- Need to know -- Ada -- Fortran 95 -- C# -- PHP -- Python/Ruby -- Need to know

C - Everything is pass-by-value - Pass-by-reference is achieved by using pointers as parameters C++ - A special pointer type called reference type for pass -by-reference - Const reference parameters gives us the efficiency of pass-by-reference with safety of pass-by-value Java - All parameters are passed by value - The value of object variables is a reference Ada - Three semantics modes of parameter transmission: in, out, in out; in is the default mode - Formal parameters declared out can be assigned but not referenced; those declared in can be referenced but not assigned; in out parameters can be referenced and assigned Fortran 95 - Parameters can be declared to be in, out, or inout mode C# - Default method: pass-by-value - Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with reference PHP: very similar to C# Python and Ruby use pass-by-assignment where the actual is assigned to the formal (all data values are objects, thus references are passed, but the objects are immutable)

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 - Ruby and Python also support variable numbers of parameters. - 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

Call-by-need (not in text)

Call-by-need is a memoized version of call-by-name • Memoization: on first reference the function is evaluated and the result is remembered. On subsequent references, the memoized result is returned without re-evaluation of the function Haskell uses call-by-need evaluation - This almost means that the language uses lazy evaluation - nothing gets evaluated until it is needed. This is advantageous for a functional language like Haskell.

What are the design considerations for parameter passing? How are they in conflict?

Considerations: 1. Efficiency 2. One-way/two-way data transfer Good programming suggest limited access to variables, which means one-way whenever possible, But pass-by-reference is more efficient to pass structures of significant size. We can use Const references to solve this.

Type Checking Parameters

Considered very important for reliability 1. FORTRAN 77 and original C: none 2. Pascal, FORTRAN 90, Java, and Ada: it is always required 3. ANSI C and C++: choice is made by the user - Prototypes enforce type checking, but variable length parameter lists are not checked 4. Relatively new languages Perl, JavaScript, and PHP do not require type checking 5. In Python and Ruby, variables do not have types (objects do), so parameter type checking is not possible at compile time

How is a multidimensional array passed as a parameter in fortran?

Formal parameter that are arrays have a declaration after the header 1. For single-dimension arrays, the subscript is irrelevant for creating the storage mapping function 2. For multidimensional arrays, the sizes are sent as parameters and used in the declaration of the formal parameter, so those variables are used in the storage mapping function

prototypes

Function declarations in C and C++ are often called

What needs to happen if a Multidimensional array is a parameter?

If a multidimensional array is passed to a subprogram and the subprogram is separately compiled, the compiler needs to know the declared size of that array to build the storage mapping function

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)

How are parameter passing methods implemented?

In most languages, parameter communication takes place thru the run-time stack Pass-by-reference are the simplest to implement; only an address is placed in the stack Pass-by-value parameters have their values copied into the stack (can be costly for non-scalars) A subtle but fatal error can occur with pass-by-reference and pass-by-value-result: a formal parameter corresponding to a constant can mistakenly be changed

User-Defined Overloaded Operators

Operators can be overloaded in Ada, C++, Python, and Ruby - Just another way to view/call overloaded functions

Pass-by-Reference (Inout Mode)

Pass an access path (i.e., a pointer or reference) • Also called pass-by-sharing Advantage: Passing process is efficient (no copying and no duplicated storage) Disadvantages 1. Slower accesses (compared to pass-by-value) to formal parameters due to extra level of indirection 2. Unwanted aliases (access broadened) • The pointers give us additional ways to access a variable (affects readability and thus reliability) • Not a problem with Pass-by-value-result

Pass by Value vs. Pass by Reference

Pass by Value: Transmit a data value by copying it Pass by Reference: Transmit an access path (a pointer or reference) to the data value

What are the two categories of subprograms?

Procedures are collection of statements that define parameterized computations • They are expected to produce side effects • Can return values only by affecting globals or parameters Functions structurally resemble procedures but are semantically modeled on mathematical functions • They are expected to return a value • They are expected to produce no side effects • In practice, program functions have side effects

How is a multidimensional array passed as a parameter in C/C++?

Programmer is required to include the declared sizes of all but the first subscript in the actual parameter 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 in terms of the size parameters (in the form of pointer arithmetic)

How is a multidimensional array passed as a parameter in Java/C#?

Similar to unconstrained arrays in Ada Arrays are objects; they are all single-dimensioned, but the elements can be arrays Each array inherits a named constant (length in Java, Length in C#) that is set to the length of the array when the array object is created

Positional Parameter Correspondence

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 Parameter Correspondence

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

General semantics of subprogram calls

The passing of parameters (with various strategies) - Allocation of local variables - Save the execution status of calling program - Transfer of control and arrange for the return - If subprogram nesting is supported, access to nonlocal variables must be arranged

subprogram linkage

The subprogram call and return operations of a language

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) Advantages: 1. Accesses are more efficient 2. Copying is cheap for scalars Disadvantages: 1. Disadvantages (if by physical move): additional storage is required (stored twice) and the actual move can be costly (for large parameters) 2. Disadvantages (if by access path method): must write-protect in the called subprogram and accesses cost more (extra level of indirect addressing)

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 Disadvantage: Require extra storage location and copy operation (just like pass-by-value in-mode)

Parameters that are Subprogram Names: Referencing Environment

When passing subprograms as parameters, we must decide how we are going to bind values to variables in that subprogram when it is invoked referencing environment: the values bound to variables

parametric polymorphism

When subprogram takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram

subprogram definition

describes the interface and the actions of the subprogram abstraction

formal parameter

is a dummy variable listed in the subprogram header and used in the subprogram

closure

is a subprogram and the referencing environment where it was defined The referencing environment is needed if the subprogram can be called from any arbitrary place in the program - A static-scoped language that does not permit nested subprograms doesn't need closures - Closures are only needed if a subprogram can access variables in nesting scopes and it can be called from anywhere - To support closures, an implementation may need to provide unlimited extent to some variables (because a subprogram may access a nonlocal variable that is normally no longer alive)

protocol

is a subprogram's parameter profile and - if it is a function - its return type

subprogram call

is an explicit request that the subprogram be executed

overloaded subprogram

is one that has the same name as another subprogram in the same referencing environment Ada,Java,C++,C# include predefined overloaded subprograms and allow users to write multiple versions of subprograms with the same name. Choice of which subprogram to call is made at compile-time Ada: the return type of an overloaded function can be used to disambiguate calls

subprogram header (or declaration)

is the first part of the definition: 1. including the name 2. the kind of subprogram 3. the formal parameters 4. return type

parameter profile (aka signature)

of a subprogram is the number, order, and types of its parameters

Overloaded subprograms

provide ad hoc polymorphism

subprogram declaration

provides the protocol - but not the body - of the subprogram

actual parameter

represents a value or address used in the subprogram call statement

generic or polymorphic subprogram

takes parameters of different types on different activations In Ada: generic subprograms are instantiated explicitly User declares a new subprogram to be a version of the generic In C++: they are instantiated implicitly, when the subprogram is named in a call or when its address is taken with the & operator


Ensembles d'études connexes

Chapter 2: Money Management Skills

View Set

Networking Final Exam Study Guide

View Set

maternal newborn trends and issues

View Set

Ch 34: Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set

Differences between Graded Potential and an Action Potential

View Set