Programming Languages Chapter 9

Programming Languages Chapter 9

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

keyword parameters

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

2 years ago

Date created

Mar 1, 2020

Cards (40)

Section 1

(40 cards)

keyword parameters

Front

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

Back

Shallow Binding

Front

The environment of the call statement that enacts the passed subprogram - Most natural for dynamic-scoped languages

Back

In what ways are coroutines different from conventional subprograms?

Front

A coroutine is a subprogram that has multiple entries and controls them itself

Back

What causes a C++ template function to be instantiated?

Front

- Versions of a generic subprogram are created implicitly when the subprogram is named in a call or when its address is taken with the & operator - Generic subprograms are preceded by a template clause that lists the generic variables, which can be type names or class names

Back

Deep Binding

Front

The environment of the definition of the passed subprogram - Most natural for static-scoped languages

Back

What is a closure?

Front

closure is a subprogram and the referencing environment where it was defined

Back

Inout-mode parameter: pass-by-reference

Front

• Pass an access path • Also called pass-by-sharing • Advantage: Passing process is efficient (no copying and no duplicated storage) • Disadvantages - Slower accesses (compared to pass-by-value) to formal parameters - Potentials for unwanted side effects (collisions) - Unwanted aliases (access broadened)

Back

Inout-mode parameter: pass-by-value-result

Front

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-value

Back

Advantages of dynamic local variables

Front

• Support for recursion • Storage for locals is shared among some subprograms

Back

What languages allow for a variable number of parameters?

Front

- 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

Back

subprogram call

Front

an explicit request that the subprogram be executed

Back

What languages allow subprogram definitions to be nested?

Front

most functional programming languages: JavaScript, Python, Ruby, and Lua

Back

subprogram protocol

Front

subprogram's parameter profile as well as its return type if it's a function

Back

Disadvantages of static local variables

Front

• Doesn't Support recursion • Storage for locals isn't shared among some subprograms

Back

What two languages allow multiple values to be returned from a function

Front

Ruby and Lua

Back

Design Considerations for Parameter Passing

Front

• Two important considerations - Efficiency - One-way or two-way data transfer • But the above considerations are in conflict - 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 In-mode parameters should be used whenever no data are to be returned through parameters to the caller. Out-mode parameters should be used when no data are transferred to the called subprogram. In-Out mode parameters should be used only when data must move in both directions between the caller and the called subprogram.

Back

Two categories of subprograms

Front

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

Back

Advantages of static local variables

Front

• Allocation/de-allocation, initialization time is quick • direct addressing • Subprograms can be history sensitive

Back

What is given in the header of a subprogram?

Front

The name, the kind of subprogram, and the formal parameters

Back

What is parametric polymorphism?

Front

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; A cheap compile-time substitute for dynamic binding

Back

actual parameters

Front

parameters as they appear in function calls student.setName('Charlie')

Back

Inout-mode parameter: pass-by-name

Front

• 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 binding • Implementation requires that the referencing environment of the caller is passed with the parameter, so the actual parameter address can be calculated

Back

Out-mode parameter: pass-by-result

Front

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

Back

What characteristic of Python subprograms sets them apart from those of other languages?

Front

function definitions are executable; in all other languages, they are non-executable

Back

What does it mean for a subprogram to be active?

Front

The subprogram has been called and is executing, but has not yet terminated

Back

In-mode parameter: pass-by-value

Front

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)

Back

Multidimensional Arrays as Parameters:

Front

• 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

Back

formal parameters

Front

parameters as they appear in function declarations public int setName(String name)

Back

Design Issues for Functions

Front

• 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 type except arrays and functions - C++ is like C but also allows user-defined types - 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 - Lua allows functions to return multiple values

Back

Overloaded Subprograms

Front

An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment adding a method with the same name but a different signature. Overloaded methods are selected at compile time based on the compile time type of the target object

Back

What languages allow the user to overload operators?

Front

Ada, C++, C#, Ruby, and Python

Back

Issues with Parameters that are Subprogram Names

Front

1. Are parameter types checked? 2. What is the correct referencing environment for a subprogram that was sent as a parameter?

Back

What are the three general characteristics of subprograms?

Front

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

Back

Disadvantages of dynamic local variables

Front

• Allocation/de-allocation, initialization time • Indirect addressing • Subprograms cannot be history sensitive

Back

positional parameters

Front

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

Back

Parameter Passing Methods of Major Languages

Front

• C - Pass-by-value - Pass-by-reference is achieved by using pointers as parameters • C++ - A special pointer type called reference type for pass-byreference • Java - All parameters are passed are passed by value - Object parameters are passed by reference • C# - Default method: pass-by-value - Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with ref • PHP: very similar to C#, except that either the actual or the formal parameter can specify ref • Perl: all actual parameters are implicitly placed in a predefined array named @_ • Python and Ruby use pass-by-assignment (all data values are objects); the actual is assigned to the forma

Back

Design issues for subprograms

Front

• 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? • Are functional side effects allowed? • What types of values can be returned from functions? • How many values can be returned from functions? • Can subprograms be overloaded? • Can subprogram be generic? • If the language allows nested subprograms, are closures supported?

Back

Argue against the C design of providing only function subprograms.

Front

C function program always returns a value readability

Back

Present one argument against providing both static and dynamic local variables in subprograms

Front

writability sucks, have to add syntax, prone to mistakes if you add new syntax, not gaining a whole lot You would have to come up with additional syntax to determine which variables are static and which are dynamic

Back

parameter profile

Front

the number, order, and types of its parameters

Back