01 Fun Prog Con
01 Fun Prog Con
What is a Program?
A program is most often viewed as a sequence of instructions for a machine An understanding of a program requires an understanding of the machine A machine language program is a sequence of instructions for a physical machine Usually represented as a sequence of 0s and 1s Not intelligible to humans A high-level language program can be viewed as a sequence of instructions for a high-level abstract machine Easier to understand because the machine is simpler Ultimately executed on a physical machine via interpretation or compilation
2
SE 2A04 focuses on programs that are sequential, terminating, subject-invoked, and applicative
Programming Languages
Programming languages are intended to facilitate program implementation but not necessarily program design There are many kinds of programming languages Imperative (Examples: Pascal, C, Basic, Fortran) Object-oriented (Examples: Smalltalk, C++, Java) Higher-order languages (Examples: Lisp, Scheme, ML) Functional (Examples: ML, Haskell) Logical (Examples: Prolog) Oberon is an imperative language with some elements of object-oriented and higher-order languages The design of a program should be tied to a specic programming language as little as possible
5
Example: Oberon
Primitive expressions: Characters, numbers, identiers Basic types Basic operators and system-supplied procedures Means of combination: Expression formation Procedure call Assignment (:=) Composition (;) Conditional selection (IF, CASE) Iteration (WHILE, REPEAT, LOOP,FOR)
Means of abstraction: Type declarations Variable and constant declarations Module and procedure declarations
7
Data Structures
A data structure is a structured collection of values Values include booleans, characters, integers, and oating-point numbers (atomic values) Values may also include some data structures (compound values) Various operators are associated with each kind of data structure: Constructors for creating data structures Selectors for retrieving the values in data structures Mutators for modifying the values in data structures Some data structures do not have mutators
10
Types
A type is a syntactic object t that denotes a set s of values t and s are often confused with each other
Types are used in a variety of ways: To classify values (latent types) To classify variables (manifest types) To control the formation of expressions To classify expressions by value Types are also used as mini-specications
11
Type Examples
Mathematical types: Z: denotes the set of integers R: denotes the set of real numbers Z R: denotes the set of functions from the integers to the real numbers Oberon types INTEGER: set of machine integers between -32768 and 32767 REAL: set of oating point numbers between -3.4E+38 and 3.4E+38 ARRAY OF CHAR: set of arrays holding characters, i.e., members of the Oberon type CHAR
12
Variables
The meaning of variable is dierent in logic, control theory, and programming In logic, a variable is a symbol that denotes an unspecied value In control theory, a variable is a changing value that is a component of the state of a system A monitored variable is a variable the system can observe but not change A controlled variable is a variable the system can both observe and change In programming, a variable is a data structure composed of a single value and with the following attributes: Name: An identier bound to the variable Value: The single value stored in the variable Type: The type of the values that can be stored
13
Oberon Variables
A variable declaration such as VAR sum: INTEGER; serves as the constructor for a variable sum is the name of the variable INTEGER is the type of the variable The value of the variable is initially empty The name of a variable (e.g., sum) serves as the selector for a variable An assignment statement such as sum := 17; serves as the mutator for a variable
14
15
Constants
The meaning of constant is dierent in logic, control theory, and programming In logic, a constant is a symbol that denotes a specied value In control theory, a constant is an unchanging value In programming, a constant is a variable without mutators The use of constants is essential for code readability and software maintenance
16
Oberon Constants
A constant declaration such as CONST pi = 3.14; serves as the constructor for a constant pi is the name of the constant 3.14 is the value of the constant The type of the constant is the type of 3.14, i.e., REAL The name of a constant (e.g., pi) serves as the selector for a constant The value of a constant cannot be changed (at run time): there is no mutator for a constant
17
Scope
The scope of an identier i bound to a value v is the region of program code in which the binding is eective The scope is usually the region of code from the place where i was rst bound to the end of the smallest enclosing block of code An identier i is only visible in its scope, i.e., outside of its scope i will normally not be bound to v If i is rebound within its scope, a new scope of i is created in which the old binding is not visible In Oberon, module and procedure declarations serve as blocks In accordance with the Principle of Least Privilege, the scope of a variable name should be as narrow as possible
18
Persistence
The persistence of a data structure (e.g., a variable) is the period of time the data structure is available to a running program Examples: The persistence of a running function procedure begins when it is called and ends when it returns a value The persistence of a variable declared in a procedure normally has the same persistence as the procedure The persistence of an Oberon module is normally from when it is rst imported to the termination of the program
19
22
2. Procedure call: <procname>(<exprlist>) 3. Return: return <expr> 4. Assignment: <varname> := <expr> 5. Composition: <statement> ; <statement>
23
25