Chapter 9 - Subroutines Flashcards

1
Q

3 characteristics of subprograms

A

1) have a single entry point
2) the caller is suspended during the execution of the called subprogram
3) control always returns to the caller when the called subprogram’s execution terminates

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

9 design issues with subprograms

A

1) what parameters passing methods are provided
2) are parameters typed checked
3) are local variables static or dynamic
4) what is the referencing environment of a passed subprogram
5) are parameter types in passed subprograms checked
6) can subprograms be overloaded
7) can subprogram definitions be nested
8) are subprograms allowed to be generic
9) is separate or independent compilation supported

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

definition of “subprogram definition”

A

a description of the actions of a subprogram abstraction

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

definition of “subprogram call”

A

an explicit request that the subprogram be executed

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

definition of “subprogram header”

A

the first line of the definition, including the name, kind of subprogram, and the formal parameters

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

definition of “parameter profile” of a subprogram

A

the number, order, and types of the subprogram’s parameters

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

definition of “protocol” of a subprogram

A

a subprogram’s parameter profile plus its return type (if it is a function)

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

definition of “active subprogram”

A

if a subprogram after being called has begun its execution but has not completed it

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

3 purposes of subroutine headers

A

indicate the syntax that follows is a subroutine

names the subroutine

enumerates the subroutine’s parameters

ex: “def” in Python/Ruby

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

definition of “subprogram declaration”

A

provides the protocol of a subprogram but not the body

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

what is formal parameter vs actual parameter

A

formal parameter: a dummy variable listed in the subprogram header and used in the body of the subprogram

actual parameter: a value or address used in the subprogram call statement

ex: def f(x): [Here ‘x’ is a formal parameter]

a = 5
f(a) [Here ‘a’ is an actual parameter]

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

definition of “parameterized computation”

A

a subprogram with parameter access to the data that it is to process

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

definition of positional parameters

A

are actual parameters that are correlated with formal parameters by their position in the subprogram header.

def f(x, y, z):
...
f (1, 2, 3) [x=1, y=2, z=3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

definition of keyword parameters

A

actual parameters that are matched to formal parameters by keyword. That is the pairing of formal parameter names to actual parameter values

def sort(list, length)
...
sort(list=a, length=n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The main disadvantage to using keyword parameters?

A

the user of the subprogram must know the names of the formal parameters

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

How to do subprogram calls with a mix of keyword and positional parameters

A

The positional parameters are placed in their correct positions, and any parameters listed after the first keyword parameter must also be keyword parameters

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

definition of default parameters

A

a default value that is used by the subprogram if no actual parameter is passed to the formal parameter in the subprogram header

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

Default parameter handling in Python vs C++

A

in python, all actual parameters after the absent one must be keyworded

ex: def f(x, y=2, z) ==> f(1, z=3) [z must be keyworded]

in C++, default parameters must be the last formal parameters in the subprogram header. Any formal parameter after the default parameter must also be default parameters

ex: int f (x, z, y=2) ==> f(1,3) [y is last parameter in header]

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

4 PLs that support procedures

A

Pascal
Ada
Modula2
FORTRAN

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

What does it mean to “fake” a procedure

A

in PLs that do not support procedures (but only support functions) the use of a “void” or “null” return value essentially mimics the behavior of a procedure

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

What are the 3 semantic models for passed parameters

A

in mode
out mode
inout mode

22
Q

What is an “in mode” parameter

A

receives data from the corresponding actual parameter

23
Q

What is an “out mode” parameter

A

transmits data to the corresponding actual parameter

24
Q

What is an “inout mode” parameter

A

receives data from the corresponding actual parameter and transmits data to the corresponding actual parameter

25
Q

What are the two conceptual models for how data is transferred by parameter

A

the value is copied (to the caller, to the called, or both ways)

an access path is transmitted (typically in the form of a pointer of reference)

26
Q

The 4 implementation models for parameter passing

A

pass-by-value
pass-by-result
pass by value-result
pass by reference

27
Q

2 characteristic of the pass-by-value model

A

in mode

normally implemented by copy

28
Q

2 ways to implement value copying for parameter transferring

A

a physical move (the value moved from one location to another using MOV)

an access path (the value is fetched by getting value located at access path)

29
Q

2 Disadvantages of both parameter copying schemes with pass-by-value

A

access path method:
1) must write protect in the called subprogram

2) accesses via indirect addressing cost more

physical move:

1) requires more storage
2) cost of moves

30
Q

Main advantage of pass-by-value

A

very fast for scalars in both linkage cost and access time

31
Q

3 characteristic of the pass-by-result model

A

out mode

local value is passed back to the caller

a physical move used

32
Q

3 characteristics of pass by value-result

A

inout mode

AKA pass-by-copy

uses physical move both ways

33
Q

2 characteristics of pass-by-reference

A

transmits an access path to (typically an address) the subprogram

the subprogram is able to access the actual parameter in calling program unit

34
Q

1 advantage and 2 disadvantages to pass-by-reference

A

Advantage:
pass process is efficient

disadvantage:

  • slower accesses
  • can allow aliasing
35
Q

What structure is used to implement parameter passing?

A

run-time stack (for both value and reference)

36
Q

definition of pass-by-assignment

A

when the actual parameter value is assigned to the formal parameter. The actual value though remains unchanged

used in python and ruby

37
Q

Biggest design consideration for parameter passing

A

one-way vs two-way parameters

one-way parameters => support Good programming practices of limited access to variables

two-way parameters => support efficiency because passing by reference is a much faster way to pass large structures

38
Q

definition of an overloaded subprogram

A

one that has the same name as another subprogram in the same referencing environment

each version of an overloaded subprogram must have a unique protocol

39
Q

How is the meaning of a call to an overloaded subprogram determined

A

by the parameter list

40
Q

Difference between templates/generic functions in C++ and Java

A

in C++ generic parameters can be primitive types. Java they cannot!

in C++, the compiler creates an explicit version of the generic function for every different set of parameters passed to it. Java creates one “raw” function of all Object types and then casts these Object types for each different set of parameters passed

Java allows restrictions on the range of classes that can be parameters. C++ does not

41
Q

independent compilation

A

the compilation of some units of the program separately from the rest of the program without the benefit of interface information

42
Q

separate compilation

A

compilation of the units of a program separately from the rest of the program using interface information to check the correctness of the interface between the two parts

43
Q

2 PLs using independent compilation

A

FORTRAN II

FORTRAN 77

44
Q

4 PLs using separate compilation

A

Ada
Modula 2
C++
Java

45
Q

3 design issues with functions

A

are side effects allowed

what type of values can be returned

how many values can be returned

46
Q

What return types ARE NOT allowed in C, C++

A

arrays

functions

47
Q

4 PLs which allow return values of ANY type

A

Ada
Python
Ruby
Lua

48
Q

3 PLs that allow functions to return MORE than 1 value

A

Ruby
Lua
F#

49
Q

definition of coroutine

A

a subprogram that has multiple entry points and controls them itself

50
Q

3 characteristics of coroutines

A

a call to a coroutine is named resume

they provide quasi-concurrent execution because their execution is interleaved and not overlapping

typically coroutines repeatedly resume each other

51
Q

how coroutines work

A

make the first call to resume which starts coroutine from its beginning (method A)

method A executes until it reaches a resume B statement

then the state of method A is saved and method B begins execution

B executes until it reaches a resume A statement

method A then executes from where it previously left off when it had called resume B.

A executes until another resume B statement

B then continues its execution from where it left off when it called resume A

[this process repeats, possibly indefinitely]