Unit 8 - Subprograms, Parameter Passing Flashcards
(47 cards)
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.