Unit 8 - Subprograms, Parameter Passing Flashcards

1
Q

Roughly speaking, a subprogram is

A

a named program block that can be called from other program blocks.

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

Java methods, Ada procedures and functions, Python, Lisp and Haskell functions are all examples of

A

subprograms

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

What are PL related examples of subprograms

A
  • Java methods
  • Ada procedures and functions
  • Python, Lisp and Haskell functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

As a tool for abstraction, a subprogram should implement

A

a single high level action that has a clearly defined effect when executed.

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

Abstraction is an

A

alternative view of a system or activity, in which some details are hidden so that a more important aspect of the system or activity stands out.

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

In a description of a subprogram aimed at the users the following aspects should be made clear:

A
  • Does the subprogram take some values as parameters? (How many and what types)
  • Does the subprogram access some external variables (ie variables whose scope is
    not confined to the subprogram block, eg global variables)? (Does it read them, modify their values.)
  • Does the subprogram read from or write to some input/output devices (eg the console, files or network)?
  • Does the subprogram return some values? (How many and what types)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Depending on the main effect, we classify subprograms as:

A

Procedures and Functions

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

Procedures =

A

Subprograms whose main purpose is to change the values of some external memory or variables and/or work with IO devices.

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

Functions =

A

Subprograms whose main purpose is to create and return a single (perhaps composite) value.

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

A pure function

A

returns a single value that depends only on the values of the function’s parameter(s) and does not modify any global (or other external) variables nor influences input/output devices.

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

A pure procedure perform effects to achieve a

A

single coherent goal and does not return any values.

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

An expression is

A

anything that can be evaluated to produce a value

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

A command

A

modifies the program state

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

A program state consists of the

A

internal state, which includes the current values of all program variables with a current lifetime, and the external state which includes the state of all its input and output devices such as keyboard, monitor, network, printer.

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

In Java, some examples of commands are:

A

assignment, creating a new object, incrementing a variable (i++).

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

In a PL which maintains a clear distinction between
expressions and commands;

A
  • evaluating an expression yields a value but does not modify program state and
  • executing a command changes state and does not produce a value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Defining a function can be viewed as

A

encapsulating an expression so that it can be replaced by a single meaningful name.

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

A thread’s lifetime is usually defined by

A

executing a single subprogram.

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

Defining a procedure can be viewed as

A

defining a new, more abstract and powerful command.

Eg input/output commands in Java are provided by the libraries
as methods (eg println), rather than primitive commands.

19
Q

In Java, the main thread

A

executes the main method.

20
Q

In Ada, the main thread

A

executes a top-level procedure defined in a single file whose name coincides with the name of that procedure.

21
Q

In Haskell, the main thread

A

evaluates and executes the main function.

22
Q

In Python and Lisp, it is a bit different: the main thread

A

reads in the given file and executes the commands in it sequentially. It is as if the whole file defined an unnamed “main” subprogram.

23
Q

A parameter bridges the calling context and the subprogram definition, allowing for

A

values to be passed either way between the two contexts.

24
Q

Parameter names, as declared in the subprogram body, are called

A

formal parameters while the expressions that are aligned with the formal parameters in a particular call are called actual parameters.

25
Q

The ways in which actual parameters and formal parameters are linked with one another divide into the following two main approaches:

A
  • Copy parameter passing mechanisms:
  • Reference parameter passing mechanisms:
26
Q

Copy parameter passing mechanisms:

A

The formal parameter is a special local variable whose lifetime starts and ends with the call.

Usually, the value computed by the actual parameter is copied at the start of the call to the formal parameter.

Sometimes, when the call is finishing, the value of the formal parameter is copied to the actual parameter (in which case the actual parameter has to be a variable).

27
Q

Reference parameter passing mechanisms:

A

The actual parameter expression has to be a single variable. The actual parameter
variable becomes the formal parameter during the call. Thus the parameter’s value is stored in the calling context and no copying of values occurs. Any access within the subprogram to the parameter results in reading from or writing to the actual parameter variable

28
Q

Copy parameter passing mechanisms

A

Copy-in parameter (also known as value parameter)

Copy-out parameter (also known as result parameter)

Copy-in-copy-out parameter (also known as value-result parameter)

29
Q

Copy-in parameter (also known as value parameter):

A

When the subprogram is called, the parameter (ie a local variable) is assigned the value given by the actual parameter.

The parameter can then be used like any other local variable – read as well as updated.

Any updates of the parameter value inside the subprogram have no effect outside the subprogram.

30
Q

Copy-out parameter (also known as result parameter):

A

When the subprogram is called, the parameter (ie a local variable) is not initialised. It must be assigned a value during the execution of the subprogram.

When the subprogram finishes, the actual parameter (which in this case must be a variable or another assignable entity) is assigned the final value of the formal parameter.

A copy-out parameter transfers a value in the opposite direction than a copy-in parameter. Also the transfer happens at the end of the subprogram call rather than in the beginning.

31
Q

Copy-in-copy-out parameter (also known as value-result parameter)

A

A combination of Copy-In and Copy-Out.

The actual parameter must be a variable or another assignable entity (a memory reference).

The formal parameter is initialised by an assignment from the actual parameter and when the call is finished, the actual parameter is assigned the final value of the formal parameter.

32
Q

Reference parameter mechanism

Advantages and disadvantages

A

In this approach a formal parameter does not have its own memory cell but shares one with the actual parameter, which must be a variable

The advantage of this approach is that there is no need for copying any values around.

The disadvantage is that the value is aliased between the two contexts, which can lead to unintuitive behaviour. I.e., when the value is modified in the subprogram, it is modified also in the calling context.

33
Q

To alleviate this problem, it is possible to forbid making any changes to a parameter that uses the reference passing mechanism. Such parameters are called

A

constant parameters

34
Q

Parameter passing in Java. Java uses

A

only copy-in (value) parameters

35
Q

Parameter aliasing. In addition to the problem of aliasing between the actual and
formal parameters, reference passing causes a more serious problem of aliasing

A

between multiple formal parameters.

If the compiler chooses to use a reference passing mechanism for arrays, then the call
Add_Vectors(A,B,C,C) which is supposed to add the three vectors A, B, C and store
the result in C will instead return the sum of A and B multiplied by two because the
parameters V3 and V_Sum become aliased, ie two names for the same variable

36
Q

An exception is an event

A

that is not expected to occur under normal circumstances.

37
Q

The occurrence of an exception has an important effect on the
execution of a subprogram

A

The thread changes its behaviour and instead of continuing in the usual manner, it searches for a handler.

38
Q

If there is no suitable exception handler in the current subprogram, the subprogram

A

terminates, usually without any option of coming back to it.

39
Q

When an exception propagates up from a call

A

the call is abandoned and no assignment of return values takes place.

40
Q

One level higher, where the call was made to the current subprogram, the same excep-
tion is thrown/raised and the thread looks for a handler in that context. Whenever this
happens, we say that the exception has

A

propagated from the subprogram call.

41
Q

The same exception behaviour is simulated in C by having the subprogram

A

return a special status value.

42
Q

A function has a side effect if it

A

reads or modifies non-local variables or input/output devices.

Anything in the behaviour of a function which cannot be described in terms
of the return value and how the return value depends on the parameter values is a
side effect.

43
Q

Pure functions are functions that are free from

A

side effects.

44
Q

Haskell does not permit the definition of any subprogram that is not a

A

pure function.

Currently it is the only PL in widespread use with this property

45
Q

An expression is referentially transparent if

A

wherever it appears in a program, it can be replaced by any equivalent expression and this will not change the meaning of the program under any circumstances.

46
Q

Functions that take a function as one of its parameters are called

A

higher-order functions.