Chapter 9 - Subroutines Flashcards Preview

Advanced Topics in Programming Languages > Chapter 9 - Subroutines > Flashcards

Flashcards in Chapter 9 - Subroutines Deck (51):
1

3 characteristics of subprograms

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

2

9 design issues with subprograms

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

3

definition of "subprogram definition"

a description of the actions of a subprogram abstraction

4

definition of "subprogram call"

an explicit request that the subprogram be executed

5

definition of "subprogram header"

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

6

definition of "parameter profile" of a subprogram

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

7

definition of "protocol" of a subprogram

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

8

definition of "active subprogram"

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

9

3 purposes of subroutine headers

indicate the syntax that follows is a subroutine

names the subroutine

enumerates the subroutine's parameters

ex: "def" in Python/Ruby

10

definition of "subprogram declaration"

provides the protocol of a subprogram but not the body

11

what is formal parameter vs actual parameter

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]

12

definition of "parameterized computation"

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

13

definition of positional parameters

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]

14

definition of keyword parameters

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)

15

The main disadvantage to using keyword parameters?

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

16

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

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

17

definition of default parameters

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

18

Default parameter handling in Python vs C++

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]

19

4 PLs that support procedures

Pascal
Ada
Modula2
FORTRAN

20

What does it mean to "fake" a procedure

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

21

What are the 3 semantic models for passed parameters

in mode
out mode
inout mode

22

What is an "in mode" parameter

receives data from the corresponding actual parameter

23

What is an "out mode" parameter

transmits data to the corresponding actual parameter

24

What is an "inout mode" parameter

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

25

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

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

The 4 implementation models for parameter passing

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

27

2 characteristic of the pass-by-value model

in mode
normally implemented by copy

28

2 ways to implement value copying for parameter transferring

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

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

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

Main advantage of pass-by-value

very fast for scalars in both linkage cost and access time

31

3 characteristic of the pass-by-result model

out mode

local value is passed back to the caller

a physical move used

32

3 characteristics of pass by value-result

inout mode

AKA pass-by-copy

uses physical move both ways

33

2 characteristics of pass-by-reference

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

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

34

1 advantage and 2 disadvantages to pass-by-reference

Advantage:
pass process is efficient

disadvantage:
-slower accesses
-can allow aliasing

35

What structure is used to implement parameter passing?

run-time stack (for both value and reference)

36

definition of pass-by-assignment

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

used in python and ruby

37

Biggest design consideration for parameter passing

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

definition of an overloaded subprogram

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

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

by the parameter list

40

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

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

independent compilation

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

42

separate compilation

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

2 PLs using independent compilation

FORTRAN II
FORTRAN 77

44

4 PLs using separate compilation

Ada
Modula 2
C++
Java

45

3 design issues with functions

are side effects allowed

what type of values can be returned

how many values can be returned

46

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

arrays
functions

47

4 PLs which allow return values of ANY type

Ada
Python
Ruby
Lua

48

3 PLs that allow functions to return MORE than 1 value

Ruby
Lua
F#

49

definition of coroutine

a subprogram that has multiple entry points and controls them itself

50

3 characteristics of coroutines

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

how coroutines work

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]