5 – Programming Paradigms
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.
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.
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?
What is a subclass?
A subclass is a class which inherits the structure and methods of another class.
A super class is a class which passes down its attributes and methods to a subclass.
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.
Sto, Dis, May, David, Minah, John are all members of one family.
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
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.
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
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.
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.
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
Procedure1
Declare a
a=5
Call Procedure2 (a)
Display a
EndProcedure1
Procedure2 (ByVal b)
b=b+2
EndProcedure2
Procedure1
Declare a
a=5
Call Procedure2 (a)
Display a
EndProcedure1
Procedure2 (ByRef b)
b=b+2
EndProcedure2
Main
Declare a
a=5
Call Procedure2
Display a
EndProcedure1
Procedure2 ( )
a=7
EndProcedure2
Main
Declare a
a=5
Call Procedure2
Display a
EndProcedure1
Procedure2 ( )
Declare a
a=7
EndProcedure2
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
Address 101
Address 201
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
Executes Function B
Reads parameter B from stack 201
101
101
Explain how the use of procedures and functions can assist a programming team when a
piece of software is being developed.
Example 1:
Answer
Example 2:
<digit> 0|1|2|3|4|5|6|7|8|9
Answer
Example 3:
Answer
Example 4:
• an alphabetic character followed by two digits, where the second digit must not be zero or
Answer
Example 5:
A variable is defined as an alphabetical character which may be followed by two digits or another
alphabetic character.
Answer
Example 6:
• an alphabetical character followed by two digits, where the first digit must be non-zero or
Answer
Example 7:
<dollar> ::= $
Answer
Example 8:
Answer
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
Answer
Syntax diagrams
Example 7:
unsigned integer
digit
digit
0