PRINCIPLE OF PROGRAMMING I
3 UNITS
LECTURE NOTES
ON
SWE 121
CONTENT
• The basic principles of programming
• The fundamentals of procedural and object-oriented
programming using suitable object-oriented language.
• Consider the principles such as:
• Procedural and data abstraction
• Encapsulation
• Code reuse and composition.
• Amble programming labs and projects from part of
this course.
2
Development of PLs
• They were influenced by hardware advances, compiler technology, users’
needs for writing high-performance programs in terms of reliability, readability,
writeability, and efficiency.
• The hardware and compiler limitations restricted early PLs to conform to
machine language in 1G PLs.
• ML is the native language of the computer used by humans to communicate
with the computer.
• MLs consist of instructions of pure binary numbers though difficult to
remember by humans.
• Next, it is the assembly language that make use of mnemonics or special
symbols to represent frequently used bit patterns.
• Assembler are used to translate an assembly language program into ML
program.
• Then, high-level PLs are developed including: imperative, object-oriented,
functional and logic paradigms.
3
Assignment 1
• Kindly, trace the evolution of programming
languages as means of communicating with
the computer.
4
INTRODUCTION
• There are several programming languages
(PLs) invented.
• Several thousands of PLs are actually put to
use.
• Natural languages are developed and evolved
independently (Igala, Ebira, Yoruba, etc.).
• PLs are far more similar to each other due to
the following:
5
INTRODUCTION
• PLs share same mathematical foundation (Boolean,
Algebra, Logic).
• They offer similar functionality (arithmetic, logic
operations, and text processing).
• They are based on the same kind of hardware and
instruction sets.
• They have common design goals, which is to find language
that make it simple for humans to use and efficient for
hardware to execute.
• Designers of PLs share their design experiences.
• They are different or similar or both.
6
Classes of PLs
• PLs can be classified according to their similarities
or the paradigms.
• Paradigm is a set of basic principles, concepts and
methods for how a computation or algorithm is
expressed.
• The major paradigms are:
o Imperative,
o Object-oriented,
o Functional,
o Logic.
7
IMPERATIVE
• Imperative is also known as procedural. It expresses
computation by fully-specified and fully-controlled
manipulation of named data in a stepwise fashion/style.
o Data or values are initially stored in variables (memory
location),
o Taken out of (read from) memory,
o Manipulated in ALU (arithmetic logic unit), and
o Stored back in the same or different variable (memory
locations),
o Finally, values of variables are sent to the I/O devices as
output.
8
IMPERATIVE
• The foundation of imperative languages is the
stored program concept-based on computer
hardware organization and architecture (von
Neumann machine).
• Typical imperative PLs include: Fortran, Algol,
Ada, Pascal and C.
9
OBJECT-ORIENTED
• Object-oriented programming paradigm is
basically the same as imperative paradigm
except that related variables, and operations
on variables are organized into classes of
objects.
• The access privileges of variables and methods
(operations) in objects can be defined to
reduce (simplify) the interaction among
objects.
10
OBJECT-ORIENTED
• Objects are considered as main building
blocks of programs, which support the
language features such as inheritance, class
hierarchy, and polymorphism.
• Typical object-oriented PPL include: Smalltalk,
C++, Java, and C#.
11
FUNCTIONAL
• Functional or applicative programming paradigm
expresses computation in terms of mathematical
functions such as ML, SML, Lisp/Scheme.
• It is different form imperative and object-oriented
programming.
• It will take a number of values as input (parameters) and
produce a single return value (output of the function).
• The return values of one or more functions as the
parameters of another function.
• The return values cannot be stored for later use.
12
LOGIC
• Logic or declarative programming paradigm
expresses computation in terms of logical
predicates.
• A logic program is a set of facts, rules and
questions.
• The execution process of a logic program is to
compare a question to each fact and rule in the
given fact and rulebase.
• Prolong is only significant logic programming
language.
13
PL features
• Orthogonality or simplicity
• Available control structures
• Data types and data structures
• Syntax design
• Support for abstraction
• Expressiveness
• Type equivalence
• Strong/weak type checking
• Exception handling
• Restricted aliasing
• Reliability
• Writeability
• Reusability
• Readability
• Efficiency
14
Functional programming
• Functional Programming is a paradigm of programming that is most similar to evaluation
of expressions in mathematics.
• In functional programming a program is viewed as an expression, which is evaluated by
successive applications of functions to their arguments, and substitution of the result for
the functional expression.
• Its origin is in the lambda calculus of Church.
• Features of FP:
– Lack of state during a computation.
– No a sequence of states.
– Created by triggers that alter the states.
– Sequence of expressions.
– Results from successive evaluation of sub-expressions.
– Results are computed values.
– No side-effects, because there are no variables to be assigned.
– Variable denote values rather than locations as in case of imperative PLs.
– No further changes can take place during computation.
– No notion of variable assignment.
15
The Elements of Programming
• A language for expressing computational processes needs:
• Primitive expressions: Expressions whose evaluation process and hence their
values are built into the language tools.
• Combination means: Create compound expressions from simpler ones.
• Abstraction: Manipulate compound objects as stand alone units.
• Reference: Reference abstracted units by their names.
• Scheme possesses these four features in an extremely clean and elegant way:
Common syntax for all composite objects: Procedure definitions, procedure
applications, collection objects: lists.
A SINGLE simple syntax for all objects that can be combined, abstracted,
named, and referenced.
Common semantic status for all composite objects, including procedures.
• This elegance is based on a uniform approach to data and procedures:
• All composite expressions can be viewed both as data and as procedures.
• It all depends on how objects are used.
• Procedures can be data to be combined, abstracted into other procedures,
named, and applied.
16
Expressions
• Scheme programs are expressions.
• There are atomic expressions and composite
expressions.
• Expressions are evaluated by the Scheme
interpreter.
• The evaluation process returns a Scheme
value, which is an element in a Scheme type.
• The Scheme interpreter operates in a read-
eval-print loop: It reads an expression,
evaluates it, and prints the resulting value.
17
Atomic expressions
• Some atomic expressions are primitive: Their
evaluation process and the returned values are
already built into Scheme semantics, and all Scheme
tools can evaluate them.
• Numbers are primitive atomic expressions in
Scheme. Eg. 345, 231, 433, etc.
• Booleans are primitive atomic expressions in
Scheme. E.g. #t.
• Primitive procedures are primitive atomic
expressions in Scheme. E.g.
+
#<procedure:+> 18
Composite expressions
• Composite expressions are called forms or combinations.
• When evaluated, the left-most expression is taken as the operator,
the rest are the operands.
• The value of the combination is obtained by applying the
procedure specified by the operator to the arguments, which are
the values of the operands.
• Scheme composite expressions are written in Prefix notation.
• Examples:
• > (+ 45 78)
123 ==== {45+78}
• > (- 56 9)
47
• > (* 6 50)
300
• > (/ 25 5)
5
• Class work--> (5 9), (*5 9), (5 * 9), (* (5 9)). Which forms can be 19
Nesting forms
• > (/ 25. 6)
4.16666666666667
• > (+ 1.5 3)
4.5
• > (+ 2 10 75.7)
87.7
• > (+ (* 3 15) (- 9 2))
52
• > (+ (* 3 (+ (* 2 4) (+ 13 5))) (+ (- 10 5) 3))
86
Or Pretty printing:
> (+ (* 3
(+ (* 2 4)
(+ 13 5)))
(+ (- 10 5)
3))
86
20
Abstraction and Reference:
(Variables and Values)
• Naming computational objects is an essential feature of any
programming language. Whenever we use naming we have a
Variable that identifies a value.
• Values turn into named objects. define is the Scheme’s special
operator for naming. It declares a variable, binds it to a value, and
adds the binding to the global environment.
• (define CUSTECH 23)
• >
• The variable CUSTECT denotes the value 23.
• The value of a define combination is void – the single value in type Void.
• A form with a special operator is called a special form.
• > size
• 6
• > (* 2 size)
• 12
• > (define a 3)
• > (+ a (* size 1.5))
• 12
• > (define result (+ a (* size 1.5)) )
• > result 21
• 12
Abstraction and Reference:
(Variables and Values)
• CUSTECH is an atomic expression but is not a primitive. define
provides the simplest means of abstraction: It allows for using
names for the results of complex operations.
• The global environment: The global environment is a function from a
finite set of variables to values, that keeps track of the name-value
bindings.
• The bindings defined by define are added to the global environment
function.
• A variable-value pair is called a binding.
• The global environment mapping can be viewed as (and
implemented by) a data structure that stores bindings.
• Characters in variable names: Any character, except space,
parentheses, “,”, “ ‘ “, and
• no “ ’ ” in the beginning. Numbers cannot function as variables.
• Note: Most Scheme applications allow redefinition of primitives. But
then we get “disastrous” results:
• > (define + (* 2 3))
• >+ 22
Abstraction and Reference:
(Variables and Values)
• in expression: (... + 2 3)
• ; in top level environment.
• > (* 2 +)
• 12
• and even:
• > (define define 5)
• > define
• 5
• > (+ 1 define)
• 6
• > (define a 3)
• . . reference to undefined identifier: a
• >
• Note: Redefinition of primitives is a bad practice, and is forbidden by
most language applications.
• Evaluation rule for define special forms (define ⟨variable⟩
⟨expression⟩): 23