2 Imperative and Procedural Programming
2 Imperative and Procedural Programming
PRINCIPLES OF
PROGRAMMING LANGUAGES
Lecture 1b:Introduction and Motivation
Structured Programming
The structure of the program text should be the guide to
understanding what it does.
Can be more efficient
Improved readability
Easier to tune and modify
Efficiency
A language must allow an underlying assignment-oriented machine
to be used directly and efficiently.
Driving factor in the implementation of many imperative languages
Types in Typical Imperative
Languages
Emphasis on data structures with assignable
components
Based on values that can be manipulated directly by
underlying machine
Size and layout fixed at compile time
Storage allocated and deallocated explicitly
Strongly typed
Storage efficiency is key
Issues with imperative programming
Imprecise semantics
Different implementations often behave differently
Portability problem
Constrained order of execution
side-effects
different execution orders give different results
difficult to introduce parallelism
Weak abstraction mechanisms
the programmer often has to consider the computer and its store
Difficulty of storage management
The updating of variables and the manual reuse of store leads to errors due
to overwritten values, dangling pointers, aliasing, etc.
Java, C#, etc. partly avoid this by garbage collection (as in FP).
What is functional programming?
At a high level: functional programming focuses on
building functions.
The programmer declares what the program does by
defining a function that maps inputs to outputs.
Complex functions are built by composing simpler
functions.
letsquare x = x*x letsumSqr(x,y) = square x + square y
Generally this means functions in the mathematical
sense:
In particular, variables are not modified by the code.
Instead variables are just names for values.
Functional Programming: Point of Departure
The programmer declares a set of data types that define the data on
which the program will operate
strong abstraction: values and store are distinct
how values are represented is left up to the implementation
The program consists of function definitions over values of these data
types: functions may be built from any well-defined operations on the
data.