Chapter 9 Flashcards

(37 cards)

1
Q

What are the three general characteristics of subprograms?

A
  • Each subprogram has a single entry point.
  • The calling program unit is suspended during the execution of the called subprogram, implying only one subprogram is in execution at any given time.
  • Control always returns to the caller when the subprogram execution terminates.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does it mean for a subprogram to be active?

A

A subprogram is considered active if it has been called and has begun execution but has not yet completed that execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is given in the header of a subprogram?

A

A subprogram header specifies that the syntactic unit is a subprogram definition, provides a name for the subprogram (if it is not anonymous), and may optionally specify a list of parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

In Python, function def statements are executable. This means a function cannot be called until its def statement has been executed, and different versions of a function can be defined based on control flow (e.g., inside an if statement)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What languages allow a variable number of parameters?

A

Languages that support a variable number of parameters include Python, Ruby, Lua, and JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a parameter profile? What is a subprogram protocol?

A
  • Parameter profile: This is the number, order, and types of a subprogram’s formal parameters.
  • Subprogram protocol: This is the parameter profile plus the return type of the subprogram (if it is a function).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Ruby array formal parameter?

A

Ruby uses array formal parameters to support a variable number of parameters, allowing a subprogram to accept any number of actual parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are formal parameters? What are actual parameters?

A
  • Formal parameters: These are the names used in the subprogram header to refer to the data it processes; they are often thought of as “dummy variables” bound to storage only when the subprogram is called.
  • Actual parameters: These are the specific values or variables provided in the subprogram call statement to be bound to the formal parameters.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantages and disadvantages of keyword parameters?

A

Advantage: They can appear in any order in the actual parameter list, which is helpful when parameter lists are long.

Disadvantage: The user of the subprogram must know the specific names of the formal parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the differences between a function and a procedure?

A

Functions: These model mathematical functions, are used to define new operations, and return a value.

Procedures: These define new statements and do not return a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the design issues for subprograms?

A
  • What parameter-passing method(s) are used?
  • Are the types of the actual parameters checked against the types of the formal parameters?
  • Are local variables statically or dynamically allocated?
  • Can subprogram definitions appear in other subprogram definitions?
  • Can subprograms be overloaded or generic?
  • If the language allows nested subprograms, are closures supported?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the advantages and disadvantages of dynamic local variables?

A

Advantages: They provide support for recursion and allow for memory conservation because storage is only allocated when the subprogram is active.

Disadvantages: They incur the overhead of allocation, recovery, and indirect addressing; additionally, they prevent subprograms from being history-sensitive (retaining values between calls).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the advantages and disadvantages of static local variables?

A

Advantages: They are very efficient because no run-time overhead is required for allocation or deallocation. Additionally, they allow subprograms to be history-sensitive, meaning they can retain values between activations.

Disadvantages: They lack support for recursion. Also, because storage is bound to the variables for the entire execution of the program, it cannot be shared with local variables of other subprograms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What languages allow subprogram definitions to be nested?

A

Languages that allow nested subprogram definitions include JavaScript, Python, Ruby, and Lua.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the three semantic models of parameter passing?

A

In-mode: The formal parameter can receive data from the actual parameter.

Out-mode: The formal parameter can transmit data to the actual parameter.

Inout-mode: The formal parameter can both receive data from and transmit data to the actual parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the modes, the conceptual models of transfer, the advantages, and the disadvantages of pass-by-value, pass-by-result, pass-by-value result, and pass-by-reference parameter-passing methods?

A
  1. Pass-by-Value (In-mode):
  • Model: The value of the actual parameter is used to initialize the corresponding formal parameter. It is normally implemented by physical copy.
  • Advantage: Fast for scalar variables in terms of link time and access time.
  • Disadvantage: Requires additional storage and copy time, which can be costly for large parameters like arrays.
  1. Pass-by-Result (Out-mode):
    - Model: No value is transmitted to the subprogram; the formal parameter acts as a local variable, and its value is copied to the actual parameter when control returns to the caller.
    - Advantage: Provides a way to return multiple values through parameters.
    - Disadvantage: Can lead to actual parameter collision (e.g., if the same variable is passed as two different result parameters).
  2. Pass-by-Value-Result (Inout-mode):
    - Model: A combination of pass-by-value and pass-by-result where the value is copied in at the start and copied back out at the end.
    - Advantage: It has the same efficiency advantages as pass-by-value for scalars while allowing data to be returned.
    - Disadvantage: Share the same storage and copy overhead issues as pass-by-value and result.
  3. Pass-by-Reference (Inout-mode):
    - Model: Rather than copying data, an access path (usually an address) is transmitted to the called subprogram.
    - Advantage: Efficient in time and space as no copying of large data (like arrays) is required.
    - Disadvantage: Access to formal parameters is slower due to indirect addressing; it can also lead to aliasing problems.
17
Q

Describe the ways that aliases can occur with pass-by-reference parameters.

A
  • Aliases can occur when two or more formal parameters are bound to the same actual parameter (e.g., sub(x, x)).
  • They also occur when a formal parameter is bound to a variable that is also visible globally or nonlocally within the subprogram.
18
Q

What is the difference between the way original C and C89 deal with an actual parameter whose type is not identical to that of the corresponding formal parameter?

A
  • In original C, the compiler did not check the types of actual parameters against the formal parameters.
  • In C89, the use of function prototypes was introduced to allow the compiler to perform static type checking on parameters.
19
Q

What are two fundamental design considerations for parameter-passing methods?

A

The two fundamental considerations are efficiency and whether the data transfer is one-way or two-way.

20
Q

Describe the problem of passing multidimensioned arrays as parameters.

A

The primary problem is that the called subprogram needs to know the sizes of all but the first dimension to correctly compute the storage mapping function (address) for any given element. Without this information, the subprogram cannot calculate where an element like a[i][j] is located in the linear memory space.

21
Q

What is the name of the parameter-passing method used in Ruby?

A

Ruby uses pass-by-value, but because all variables are references to objects, it is effectively “pass-by-value where the value is a reference” (often called pass-by-sharing)

22
Q

What are the two issues that arise when subprogram names are parameters?

A
  • The first issue is type checking: whether the types of the parameters of the passed subprogram can be checked.
  • The second issue is the referencing environment: which environment (the one where the subprogram was defined, the one where it was passed, or the one where it is called) should be used during its execution
23
Q

Define shallow and deep binding for referencing environments of subprograms that have been passed as parameters

A
  • Shallow binding: The referencing environment is the environment of the call statement that enacts the passed subprogram.
  • Deep binding: The referencing environment is the environment in which the passed subprogram was defined.
24
Q

What is an overloaded subprogram?

A

An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. Every version of an overloaded subprogram must have a unique protocol (parameter profile).

25
What is parametric polymorphism?
Parametric polymorphism occurs when a subprogram takes generic parameters that are used in type expressions that describe the types of the subprogram’s parameters. It allows a single piece of code to work for multiple types.
26
What causes a C++ template function to be instantiated?
A C++ template function is instantiated when the compiler encounters a call to the function with a particular set of actual parameter types, or when the address of a specific instance of the template is taken.
27
In what fundamental ways do the generic parameters to a Java 5.0 generic method differ from those of C++ methods?
In Java 5.0, generic parameters must be classes (they cannot be primitives). Additionally, Java uses "bounds" on its generic parameters (e.g., T extends Comparable), whereas C++ templates do not traditionally use explicit bounds in the same syntactic way.
28
If a Java 5.0 method returns a generic type, what type of object is actually returned?
The Java compiler uses a process called erasure, where generic type parameters are replaced by their bounding type (often Object). Therefore, the method actually returns an object of the bounding type (usually Object).
29
If a Java 5.0 generic method is called with three different generic parameters, how many versions of the method will be generated by the compiler?
Only one version of the method is generated by the compiler, regardless of the number of different generic parameters used. This is due to the process of erasure, which differs from C++, where a new version is created for each unique set of types.
30
What are the design issues for functions?
- Are side effects allowed? - What types of return values are allowed?
31
What two languages allow multiple values to be returned from a function?
Lua and Python are identified as languages that allow multiple values to be returned from a function.
32
What exactly is a delegate?
In C#, a delegate is a type-safe object that references a method. It is the C# equivalent of a function pointer, but is more secure and object-oriented.
33
What is the main drawback of generic functions in F#?
The main drawback is that they sometimes require explicit type annotations because the type inference system cannot always resolve the correct type for certain operations, such as arithmetic, which are not globally generic
34
What is a closure?
A closure is a subprogram and the referencing environment where it was defined. This environment remains "attached" to the subprogram even when the subprogram is called from outside that environment.
35
What are the language characteristics that make closures useful?
Closures are useful in languages that allow nested subprograms and allow subprograms to be returned as values or passed as parameters, particularly when those subprograms can access non-local variables
36
What languages allow the user to overload operators?
Languages that allow the user to overload operators include C++, C#, Ada, Python, and Ruby.
37
In what ways are coroutines different from conventional subprograms?
Unlike conventional subprograms, which have a master-slave relationship, coroutines are peers. A coroutine can be paused (at a yield or similar point) and later resumed at the point where it left off, rather than starting from the beginning. This means they have multiple entry points.