Lecture on Programming Languages Chapter#5. Programming Paradigms/Models Paradigm defines a model or...

Post on 02-Jan-2016

220 views 1 download

Tags:

Transcript of Lecture on Programming Languages Chapter#5. Programming Paradigms/Models Paradigm defines a model or...

Lecture on Programming

LanguagesChapter#5

Programming Paradigms/Models

Paradigm defines a model or way of programming

We will discuss the following three paradigms Imperative/Procedural Paradigm Declarative Paradigm Object Oriented Paradigm

The evolution of programming paradigms

6-3

The composition of a typical imperative program or program unit

6-4

Procedural Units

Local versus Global Variables Formal versus Actual Parameters Passing parameters by value versus

reference Procedures versus Functions

6-5

The flow of control involving a procedure

6-6

Example of procedure written in C language

6-7

Executing the procedure

Demo and passing

parameters by value

Executing the

procedure Demo and passing

parameters by reference

Object Oriented Paradigm

In this approach all real world things are considered as an object. Each object has a certain set of qualities/ attributes and each object can perform some job/method. Thus if I have an object CAR then Car has its color, engine#, make, model etc as attributes and its methods can be drive(), stop() etc

Examples include C++, Java etc

Declarative Programming Approach

It emphasizes the question “What is the problem?” rather than “What algorithm is required for solving the problem?” Here a general problem solving approach is developed that can solve a number of problems. These languages are difficult to design and are special purpose by nature

PROLOG (Programming LOGic) is an example

Figure 6.25 Resolving the statements (P OR Q), (R OR ¬Q), ¬R, and ¬P

6-12

Declarative Programming

Resolution: Combining two or more statements to produce a new statement (that is a logical consequence of the originals). Example: (P OR Q) AND (R OR Q)

resolves to (P OR R) Resolvent: A new statement deduced by

resolution Clause form: A statement whose elementary

components are connected by the Boolean operation OR

Unification: Assigning a value to a variable so that two statements become “compatible.” 6-13

Prolog Fact: A Prolog statement establishing a

fact Consists of a single predicate Form: predicateName(arguments).

Example: parent(bill, mary). Rule: A Prolog statement establishing a

general rule Form: conclusion :- premise.

:- means “if” Example: wise(X) :- old(X). Example: faster(X,Z) :- faster(X,Y), faster(Y,Z).

6-14