You are on page 1of 12

Chapter 3.

5 – Programming Paradigms

The following programming paradigms are discussed in this chapter:

1. Machine language
2. Assembly language
3. Procedural languages (third generation languages)
4. Declarative languages (fourth generation languages)
5. Object oriented languages

Machine language

In machine language, programs are written in 1s and 0s. The processor can understand only the
machine language. Machine language is the lowest level programming language. The
disadvantage with machine language is that it is extremely tedious to write and maintain
programs with 1s and 0s.

Assembly language

Assembly languages are machine oriented, which means that different computer architectures
have different versions of assembly languages. In assembly language, program code is written in
mnemonics and addresses with labels.

An assembly language program that adds up two numbers and stores the result is given below.

Label Function Address Comments


LDA X Load the accumulator with the value of X
ADD Y Add the value of Y to the accumulator
STA Z Store the result in Z
STOP Stop the program
X: 20 Value of X = 20
Y: 35 Value of Y = 35
Z: Location for result

Problem with the assembly language is that coding is error-prone, difficult to debug, correct and
maintain.

Procedural languages

Procedural languages are problem oriented, which means that there are procedural languages
which can be used to create programs for specific purposes.
2

Programs coded with procedural programming languages describe the step by step procedure that
should be followed to solve a problem.

Code in procedural programming languages consists of keywords appropriate for the type of
problem being solved.

For example, COBOL (Common Business Oriented Language) is used to develop programs for
many business applications and its code consists of terms such as file, move and copy. Other
examples of procedural languages are FORTRAN (FORmula TRANslation) and ALGOL
(ALGOrithmic Language) which were developed mainly to develop solutions for scientific and
engineering problems.

Problem with the procedural languages is that it can be difficult to reuse code and to modify
solutions when better methods of solution are developed.

Declarative languages

A program written in a fourth generation language is used by the user telling it what the problem
is. It doesn’t have the steps necessary to solve the problem.

A program written in a declarative language searches a database according to a set of rules


supplied to it and produces the results.

An example of a declarative programming language is Prolog.

Object oriented languages

In object oriented languages, programs are developed as classes. Classes include reusable code. A
class has the three features called data encapsulation, inheritance and polymorphism.

What is a class?

A class is a template for creating objects.

What is a subclass?

A subclass is a class which inherits the structure and methods of another class.

What is a super class?

A super class is a class which passes down its attributes and methods to a subclass.

What is data encapsulation?


3

The technique of hiding the internal implementation details of an object from its external view is
called data encapsulation. Therefore data can only be accessed by the methods or attributes
provided by the class.

What is inheritance?

The fact that when a child class is derived from the parent class, the child class would contain all
the properties (attributes) and methods (operations) of the parent class is called as inheritance.
The parent class is also called as the base class. This helps the code in a class to be reused.

What is polymorphism?

The fact that when two or more classes that are inherited from a parent class, implementing an
inherited method differently is called polymorphism.

An example of a prolog program

Sto, Dis, May, David, Minah, John are all members of one family.

The following facts apply:

female(sto).
female(may).
female(minah).
male(john).
male(dis).
male(david).
parent(john,dis).
parent(john,may).
parent(dis,sto).
parent(dis,david).
parent(minah,dis).
parent(minah,may).

Where

male(X).states that X is male


female(X).states that X is female
parent(X,Y).states that X is a parent of Y
mother(X,Y) :- parent(X,Y), female(X).states that X is mother of Y if X is parent of Y and X is
female. (This is a rule)
father(X,Y) :- parent(X,Y),male(X).states that X is father of Y if X is parent of Y and X is male.
(This is a rule)
grandparent (X,Y) :- parent(X,Z),parent(Z,Y).

By using examples from the facts given, explain what is meant by instantiation?
4

Instantiation means assigning a value to a variable. Example: The query male(X) instantiates X to
john.

By using examples from the facts given, explain what is meant by goal?

Goal means the intention to find all the instances that satisfy a rule. If the rule is male(X) then the
goal is to find john, dis and david.

By using examples from the facts given, explain what is meant by backtracking?

If the result of one rule, does not apply in a second rule, then going back to find another result of
the first rule is called backtracking. For example, parent (john,dis) is found if we are searching for
mother of dis.
This fails the second part of the rule for mother because john is male, so backtracking is used to
return to the next example satisfying the first part of the rule.

Write a rule to define grandmother

grandmother (x,y) :- grandparent (x,y), female (x)

Explain how the above new rule is used to find the grandmother of david

1. Ignores parent (john,dis) parent (john,may) parent (dis,sto) because Y<> david
2. Finds parent (dis,david)
3. Searches for parent(X,dis), finds X = john
4. Finds male(john), therefore rejects X = john because not female
5. Backtracks to find next occurrence of parent(X,dis)
6. Finds parent(minah,dis)
7. Finds female(minah), reports minah is grandmother

What is meant by local variable?

This is a variable declared within a procedure or a function of a program and which cannot be
accessed by code outside that procedure or function. A Local variable can only be accessed by
code within the procedure or the function where it is declared.

What is meant by global variable?

A global variable is declared within the main program or within a public module of a program. It
can be accessed from anywhere within the source code of the program.

What is meant by a parameter passed by value?


5

1. Only the value of the parameter is passed to the procedure called.


2. Value of the parameter can be manipulated by the procedure called.
3. When the procedure called is terminated the new value is discarded and the value of the
parameter in the calling procedure returns to the original value

What is meant by a parameter passed by reference?

1. The parameter is stored in the original location and only a pointer (a reference to the
parameter’s memory address) is passed to the procedure called.
2. Any changes made to the reference passed to the procedure called, will remove the value of the
parameter at the original location.
3. When the procedure called is terminated the new value is available to the calling procedure

Example to illustrate passing a parameter by value

Procedure1
Declare a
a=5
Call Procedure2 (a)
Display a
EndProcedure1

Procedure2 (ByVal b)
b=b+2
EndProcedure2

The output of the parameter will be the display of value 5

Example to illustrate passing a parameter by reference

Procedure1
Declare a
a=5
Call Procedure2 (a)
Display a
EndProcedure1

Procedure2 (ByRef b)
b=b+2
EndProcedure2

The output of the parameter will be the display of value 7

Example to illustrate local and global variables


6

Main
Declare a
a=5
Call Procedure2
Display a
EndProcedure1

Procedure2 ( )
a=7
EndProcedure2

The output will be the display of value 7

Example to illustrate local and global variables

Main
Declare a
a=5
Call Procedure2
Display a
EndProcedure1

Procedure2 ( )
Declare a
a=7
EndProcedure2

The output will be the display of value 5

Explain how a stack is used to handle procedure calling and parameter passing?

1. The return address is placed on stack along with the values of parameters when a procedure is
called. If that procedure in turn calls another procedure the returning addresses are placed on
the stack in the order so that most recent return address is placed on the top of the stack.

2. As the procedure(s) execute the parameters are read off the stack

3. The return values will be passed to the statements by taking the addresses out of those
statements out of the stack.
7

Main Program

Call Function A (Parameter A)

Address 101

End Main Function A (Parameter A)

Call Function B (Parameter B)

Address 201

Return Function B (Parameter B)

Return
Call Function A
Push Return Address 101 onto stack Parameter A
Push parameter A onto stack
101

Executes Function A
Reads parameter A from stack
101

Call Function B Parameter B


Push Return Address 201 onto stack 201
Push parameter B onto stack
101

Executes Function B
Reads parameter B from stack 201
101

Return from Function B


Pops the return address 201 from stack

101

Return from Function A


Pops the return address 101 from stack
8

Explain how the use of procedures and functions can assist a programming team when a
piece of software is being developed.

1. Individual expertise of each programmer can be utilized


2. Errors are far more easily spotted
3. Each procedure or function is much simpler to solve than the original problem
4. Individual procedures are easier to test than the whole solution
5. Library routines can be utilized
6. One procedure can be used multiple times
7. Functions are mathematically provable to be correct/faulty

Backus Naur form

Example 1:

An unsigned integer is defined as an unlimited set of digits.

Use BNF to define <unsigned integer>

Answer

<unsigned integer> ::= <digit>|<digit><unsigned integer>

<digit> ::= 0|1|2|3|4|5|6|7|8|9

Example 2:

An integer is defined as an unsigned integer or a signed integer. An unsigned integer is one or an


unlimited set of digits, where a digit is defined as:

<digit> 0|1|2|3|4|5|6|7|8|9

Use BNF to define <integer>

Answer

<integer> ::= <unsigned integer>|<signed integer>

<signed integer> ::= +<unsigned integer>|–<unsigned integer>

<unsigned integer> ::= <digit>|<digit><unsigned integer>

<digit> ::= 0|1|2|3|4|5|6|7|8|9


9

Example 3:

A variable is defined in a particular programming language as an alphabetic character which may


be followed by two digits or another alphabetic character.

Alphabetic characters and digits are defined as:

<alpha> ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

<digit> ::= 0|1|2|3|4|5|6|7|8|9

Use BNF and other definitions to define variable.

Answer

<variable> ::= <alpha>|<alpha><digit><digit>|<alpha><alpha>

Example 4:

A variable is defined in a particular programming language as either

• an alphabetic character followed by two digits, where the second digit must not be zero or

• an unlimited set of alpha characters

Alphabetic characters and digits are defined as:

<alpha> ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

<digit> ::= 0|1|2|3|4|5|6|7|8|9

Use BNF and other definitions to define the variable.

Answer

<variable> ::= <x>|<y>

<x> ::= <alpha><digit><NZD>

<y> ::= <alpha>|<alpha><y>

<NZD> ::= 1|2|3|4|5|6|7|8|9

Example 5:

A variable is defined as an alphabetical character which may be followed by two digits or another
alphabetic character.

Alphabetic characters and digits are defined as


10

<alpha> ::= A|BC|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

<digit> ::= 0|1|2|3|4|5|6|7|8|9|

Use BNF to define the variable.

Answer

<variable> ::= <alpha>|<alpha><digit><digit>|<alpha><alpha>

Example 6:

A variable is defined as:

• an alphabetical character followed by two digits, where the first digit must be non-zero or

• an unlimited set of alpha characters

Alphabetic characters and digits are defined as

<alpha> ::= A|BC|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

<digit> ::= 0|1|2|3|4|5|6|7|8|9

Use BNF to define the variable.

Answer

<variable> ::= <X>|<Y>

<X> ::= <alpha><nzd><digit>

<Y> ::= <alpha>|<alpha><Y>

<nzd> ::= 1|2|3|4|5|6|7|8|9|

Example 7:

A variable is defined as one or many unsigned digits followed by a $ sign

Unsigned digit and the dollar sign are defined as

<unsigned digit> ::= 0|1|2|3|4|5|6|7|8|9

<dollar> ::= $

Use BNF to define the variable.


11

Answer

<variable> ::= <x><dollar>

<x> ::= <unsigned digit>|<unsigned digit><x>

Example 8:

A variable is defined as:

• One or two letters, followed by

• Any number of digits (including zero) followed by either a

 $ sign if there are no digits

 & sign if there are any digits

Use BNF to define the variable.

Answer

<variable> ::= <x>|<y>

<x> ::= <s><dollar>|<t><dollar>


<s> ::= <alpha>
<t> ::= <alpha><alpha>

<y> ::= <m><ampersand>|<n><ampersand>


<m> ::= <alpha><p>
<n> ::= <alpha><alpha><p>
<p> ::= <digit>|<digit><p>

<alpha> ::= A|BC|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z


<digit> ::= 0|1|2|3|4|5|6|7|8|9

Example 9:

A variable is defined as one or more letters, followed by a $ sign, which in turn is followed by
one or more digits, followed by an & sign

Use BNF to define the variable.

Answer

<variable> ::= <x><y>


12

<x> ::= <p><dollar>


<p> ::= <alpha>|<alpha><p>

<y> ::= <q><ampersand>


<q> ::= <digit>|<digit><q>

<alpha> ::= A|BC|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z


<digit> ::= 0|1|2|3|4|5|6|7|8|9

Syntax diagrams

Syntax diagram is an alternative means of BNF for describing program syntax.

Example 7:

Describe the definition of an unsigned integer by a syntax diagram.

unsigned integer
digit

digit
0

You might also like