Unit 8 - Subprograms, Parameter Passing Flashcards
Roughly speaking, a subprogram is
a named program block that can be called from other program blocks.
Java methods, Ada procedures and functions, Python, Lisp and Haskell functions are all examples of
subprograms
What are PL related examples of subprograms
- Java methods
- Ada procedures and functions
- Python, Lisp and Haskell functions
As a tool for abstraction, a subprogram should implement
a single high level action that has a clearly defined effect when executed.
Abstraction is an
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.
In a description of a subprogram aimed at the users the following aspects should be made clear:
- 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)
Depending on the main effect, we classify subprograms as:
Procedures and Functions
Procedures =
Subprograms whose main purpose is to change the values of some external memory or variables and/or work with IO devices.
Functions =
Subprograms whose main purpose is to create and return a single (perhaps composite) value.
A pure function
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.
A pure procedure perform effects to achieve a
single coherent goal and does not return any values.
An expression is
anything that can be evaluated to produce a value
A command
modifies the program state
A program state consists of the
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.
In Java, some examples of commands are:
assignment, creating a new object, incrementing a variable (i++).
In a PL which maintains a clear distinction between
expressions and commands;
- evaluating an expression yields a value but does not modify program state and
- executing a command changes state and does not produce a value
Defining a function can be viewed as
encapsulating an expression so that it can be replaced by a single meaningful name.
A thread’s lifetime is usually defined by
executing a single subprogram.
Defining a procedure can be viewed as
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.
In Java, the main thread
executes the main method.
In Ada, the main thread
executes a top-level procedure defined in a single file whose name coincides with the name of that procedure.
In Haskell, the main thread
evaluates and executes the main function.
In Python and Lisp, it is a bit different: the main thread
reads in the given file and executes the commands in it sequentially. It is as if the whole file defined an unnamed “main” subprogram.
A parameter bridges the calling context and the subprogram definition, allowing for
values to be passed either way between the two contexts.
Parameter names, as declared in the subprogram body, are called
formal parameters while the expressions that are aligned with the formal parameters in a particular call are called actual parameters.
The ways in which actual parameters and formal parameters are linked with one another divide into the following two main approaches:
- Copy parameter passing mechanisms:
- Reference parameter passing mechanisms:
Copy parameter passing mechanisms:
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).
Reference parameter passing mechanisms:
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
Copy parameter passing mechanisms
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)
Copy-in parameter (also known as value parameter):
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.
Copy-out parameter (also known as result parameter):
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.
Copy-in-copy-out parameter (also known as value-result parameter)
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.
Reference parameter mechanism
Advantages and disadvantages
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.
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
constant parameters
Parameter passing in Java. Java uses
only copy-in (value) parameters
Parameter aliasing. In addition to the problem of aliasing between the actual and
formal parameters, reference passing causes a more serious problem of aliasing
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
An exception is an event
that is not expected to occur under normal circumstances.
The occurrence of an exception has an important effect on the
execution of a subprogram
The thread changes its behaviour and instead of continuing in the usual manner, it searches for a handler.
If there is no suitable exception handler in the current subprogram, the subprogram
terminates, usually without any option of coming back to it.
When an exception propagates up from a call
the call is abandoned and no assignment of return values takes place.
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
propagated from the subprogram call.
The same exception behaviour is simulated in C by having the subprogram
return a special status value.
A function has a side effect if it
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.
Pure functions are functions that are free from
side effects.
Haskell does not permit the definition of any subprogram that is not a
pure function.
Currently it is the only PL in widespread use with this property
An expression is referentially transparent if
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.
Functions that take a function as one of its parameters are called
higher-order functions.