You are on page 1of 6

Lecture 03 Programming Paradigms

A programming paradigm is a fundamental style of computer programming. Paradigms differ


in the concepts and abstractions used to represent the elements of a program (such as objects,
functions, variables, constraints, etc.) and the steps that compose a computation (assignment,
evaluation, continuations, data flows, etc.).

A programming paradigm provides (and determines) the view that the programmer has of the
execution of the program. E.g. in object-oriented programming, programmers can think of
a program as a collection of interacting objects, while in functional programming a program
can be thought of as a sequence of stateless function evaluations. Different programming
languages advocate different programming paradigms. Some languages are designed to
support one particular paradigm (Smalltalk and Java support object-oriented programming
while Haskell and Scheme support functional programming), while other programming
languages support multiple paradigms (such as Common Lisp, Python, and Oz.)

The relationship between programming paradigms and programming languages can therefore
be complex since a programming language can support multiple paradigms. For example, C+
+ is designed to support elements of procedural programming, object-based programming,
object-oriented programming, and generic programming.
However, designers and programmers decide how to build a program using those paradigm
elements i.e. one can write a purely procedural program in C++, one can write a purely
object-oriented program in C++, or one can write a program that contains elements of both
paradigms.

Examples of Programming Paradigms:

• Unstructured Programming • Component-Oriented Programming


• Structured Programming • Aspect-Oriented Programming
• Imperative Programming • Rule-Based Programming
• Declarative Programming • Table-Oriented Programming
• Message Passing Programming • Pipeline Programming
• Procedural Programming • Object-Based Programming
• Functional Programming • Post-Object Programming
• Value-Level Programming • Subject-Oriented Programming
• Function-Level Programming • Reflective Programming
• Flow-Driven Programming • Dataflow Programming
• Event-Driven Programming • Policy-Based Programming
• Scalar Programming • Annotative Programming
• Array Programming • Attribute-Oriented Programming
• Class-Based Programming • Concept-Oriented Programming
• Prototype-Based Programming • Declarative Programming
• Object-Oriented Programming • Language-Oriented Programming
• Constraint Programming • Ars Based Programming
• Logic Programming • Grammar-Oriented Programming
A. Unstructured Programming
Unstructured programming is a programming paradigm where all code is contained in a
single continuous block. Unstructured programming languages have to rely on execution flow
statements such as Goto, used in many languages to jump to a specified section of code e.g.
Assembly Language programming.

Unstructured source code is notoriously difficult to read and debug, and so is discouraged in
programming languages that support any kind of structure. However, structure is not needed
in any programming language since program structures can always be implemented by a
combination of conditional statements and Goto statements.

Unstructured programming is still used in some scripting languages such as MS-DOS batch
files, older programming languages such as BASIC or FORTRAN. Despite Goto statements
(jumps) having a small performance benefit over procedure calls, current CPU architectures
have made the difference negligible. In fact, the improper use of such statements can be
harmful, either by obfuscating code and/or preventing good compiler optimization.

B. Structured Programming
Structured programming is often associated with a "top-down" approach to design. In this
way designers map out the large scale structure of a program in terms of smaller
operations, implement and test the smaller operations, and then tie them together into a
whole program.
Almost any language can use structured programming techniques to avoid common pitfalls of
unstructured languages and it is most famous for removing or reducing reliance on the GOTO
statement.

Low-level structure
At a low level, structured programs are composed of simple, hierarchical program flow
structures. These are regarded as single statements, and are at the same time ways of
combining simpler statements, which may be one of these structures, or primitive statements
such as assignments or procedure calls. The three types of structure identified by Dijkstra
were sequence, selection, and repetition:

 "Sequence" refers to a sequence of statements executed in order.


 In "selection", one of a number of statements is executed depending on the state of the
program. This is usually expressed with keywords such as if..then..else..endif, switch, or
case.
 In "repetition" a statement is executed until the program reaches a certain state or applied
to every element of a collection. This is usually expressed with keywords such as while,
repeat, for or do..until. Often it is recommended that each loop should only have one entry
point (and in the original structural programming, also only one exit point), and a few
languages enforce this

High-level structure
Coders should break larger pieces of code into shorter subroutines (functions, procedures.
methods, blocks, or otherwise) that are small enough to be understood easily. In general,
programs should use global variables sparingly; instead, subroutines should use local
variables and take arguments by either value or reference. These techniques help to make
isolated small pieces of code easier to understand without having to understand the whole
program at once.

C. Procedural Programming
Procedural programming is a programming paradigm based upon the concept of the
procedure call. Procedures, also known as routines, subroutines, methods, or functions simply
contain a series of computational steps to be carried out. Any given procedure might be called
at any point during a program's execution, including by other procedures or itself
(Recursion).
The main program here coordinates calls to procedures
and hands over appropriate data as parameters.

Procedural programming is often a better choice than simple sequential or unstructured


programming in many situations which involve moderate complexity or which require
significant ease of maintainability. Possible benefits:
 The ability to re-use the same code at different places in the program without copying it.
 An easier way to keep track of program flow than a collection of "GOTO" or "JUMP"
statements (which can turn a large, complicated program into so-called "spaghetti code").
 The ability to be strongly modular or structured.

Procedural Programming Languages


To be considered procedural, a programming language should support procedural
programming by having an explicit concept of a procedure, and syntax to define it. It should
ideally support specification of argument types, local variables, recursive procedure calls and
use of procedures in separately built program constructs. It may also support distinction of
input and output arguments.

Examples:
• Ada • ColdFusion
• ALGOL • COBOL
• BASIC • Component Pascal
• C • D
• C++ • Delphi
• ECMAScript a.k.a. ActionScript, • Occam
DMDScript, JavaScript, JScript • M
• Forth • Pascal
• Fortran • Perl
• F • PHP
• Lasso • PL/C
• Maple • PL/I
• Mathematica • Rapira
• MATLAB • Seed7
• Modula-2 • VBScript
• Oberon and Oberon-2 • Visual Basic

D. Object-Oriented Programming (OOP)


The idea behind object-oriented programming is that a computer program may be seen as
comprising a collection of individual units, or objects, that act on each other, as opposed to a
traditional view in which a program may be seen as a collection of functions, or simply as a
list of instructions to the computer. Each object is capable of receiving messages, processing
data, and sending messages to other objects.

Advantages of OOP
 OOP is claimed to promote greater flexibility and maintainability in programming, and is
widely popular in large-scale software engineering.
 OOP is claimed to be easier to learn for those new to computer programming than
previous approaches.
 The OOP approach is often simpler to develop and to maintain, lending itself to more
direct analysis, coding, and understanding of complex situations and procedures than other
programming methods.

Fundamental Concepts of OOP


Object-oriented programming (OOP) emphasizes the following concepts:
1. Class - the unit of definition of data and behaviour (functionality) for some kind-of-thing.
A class is the basis of modularity and structure in an object-oriented computer program.
2. Object - an instance of a class. An object is the run-time manifestation (instantiation) of a
particular class. Each object has its own data, though the code within a class (or a subclass
or an object) may be shared for economy.
3. Method (also known as message) - how code can use an object of some class. A method is
a form of subroutine operating on a single object. Methods may be divided into queries
returning the current state and commands changing it. Sometimes access to the data of an
object is restricted to the methods of its class.
4. Inheritance - a mechanism for creating subclasses. Inheritance provides a way to define a
(sub)class as a specialization or subtype or extension of a more general class. A subclass
inherits all the members of its superclass(es), but it can extend their behaviour and add
new members. Inheritance is the "is-a" relationship.
5. Encapsulation - ensuring that code outside a class sees only functional details of that class,
but not implementation (how it does it) details. The latter are liable to change, and could
allow a user to put an object in an inappropriate state. Encapsulation is achieved by
specifying which classes may use the members of an object. The result is that each object
exposes to any class a certain interface - those members accessible to that class.
6. Member - of a class or object is a method or a data item describing the state of an object.
In some languages the general term is feature.
Members are often specified as public, protected and private, determining whether they
are available to all classes, sub-classes or only the defining class. Some languages go
further: Java uses the protected keyword to restrict access also to classes in the same
package, C# and VB.NET reserve some members to classes in the same assembly using
keywords internal (C#) or Friend (VB.NET).
7. Abstraction - the ability of a program to ignore the details of an object's (sub)class and
work at a more generic level when appropriate.
8. Polymorphism - polymorphism is behavior that varies depending on the class in which the
behavior is invoked, that is, two or more classes can react differently to the same message.

E. Flow-Driven Programming
Flow-driven programming is a computer programming paradigm used by traditional
programs, which follow their own control flow pattern, only sometimes changing course at
branch points. This is in contrast to Event-driven programming, which is especially common
in applications which are asynchronous, such as a text editor, a kernel, or a GUI application.

F. Event-Driven Programming
Event-driven programming is a computer programming paradigm whereby unlike traditional
programs, which follow their own control flow pattern, only sometimes changing course at
branch points, the control flow of event-driven programs is largely driven by external
events.

Instead of waiting for a complete command which may order it to process information, the
system is pre-programmed with an event loop, to look repeatedly for information to process
(whether this might be the appearance of a file in a folder, a keyboard or mouse operation, or
a timer event) and then perform a trigger function to process it. Programming an event driven
system is thus a matter of rewriting the default trigger functions of the system, to match the
required behavior.

The method by which information on events is acquired by the underlying system is


immaterial. Inputs can be polled in the event loop, or interrupt handlers can be registered to
react to hardware events; many systems use a mixture of both techniques. The
preprogrammed algorithm ensures that triggers provided are executed when they are needed,
thus providing a software abstraction that emulates an interrupt driven environment.
Graphical User Interface programs are typically programmed in an event-driven style.
Examples of Computer Programming Languages

 C is a compiled procedural, imperative programming language made popular as the basis


of UNIX.
 C++ is a compiled programming language based on C, with support for object-oriented
programming. It is one of the most widely-used programming languages currently
available. It is often considered to be the industry-standard language of game
development, but is also very often used to write other types of computer software
applications. C++ was developed by Bjarne Stroustrup and retains the syntax and many
familiar functions of C, but also adds various concepts associated with other programming
paradigms, such as classes.
 Java is an object oriented interpreted programming language. It has gained popularity in
the past few years for its ability to be run on many platforms, including Microsoft
Windows, Linux, Mac OS, and other systems. It was developed by Sun Microsystems.
 Lisp is a family of functional, sometimes scripted, programming languages often used in
AI (Artificial Intelligence).
 Pascal is a general-purpose structured language named after the famous mathematician
and philosopher Blaise Pascal. It was very popular during the 80's and 90's.
 BASIC was the most used at the early times of microcomputers, in the seventies and
eighties, but was further replaced by more powerful languages: C, Pascal, Java, etc...
 Visual Basic designed and developed by Microsoft, is integrated into a visual development
interface.

Homework

1. Download the Dev C++ compiler from


http://www.toggle.com/lv/software/download/kl7863.htm

2. Download and read through the manual on how to use Dev C++ from
http://coltech.vnu.edu.vn/~duybt/ap/How_to_use_Dev-C.pdf

Homework (Chapter 5)

Write a program that calculates and returns the area and circumference of a circle and the
area and perimeter of a triangle. In both cases the user is allowed to specify dimensions.

You might also like