More on programming paradigms
The terms procedural, declarative and OOP
Languages can also be classified according to their type as well as their generation. We will consider a range of paradigms below.
There are a number of important programming paradigms to consider. These include:
-
- Procedural languages
- Declarative languages
- Object oriented languages
- Functional languages
- Mark-up languages
- Visual programming languages
Procedural languages (also known as Imperative languages)
With these kinds of languages, you must tell the computer exactly what to do, instruction by instruction, in the same way that Von Neumann processors work through programs, instruction by instruction. You will see later that this is not the only way to write a program! This type of language has its roots firmly in the design of computers, digital technology and the Von Neumann processor. The computer will start at the first instruction, carry out the command and then move on to the next one. The programmer writes programs and controls program flow by using only three basic building blocks:
-
- Sequence.
- Selection.
- Iteration.
In Pascal, conditional branching (selecting which code to do) is possible using the IF statement and the CASE statement. Iteration is possible using FOR, WHILE and REPEAT. Procedural languages also make heavy use of functions and procedures, which are the main building blocks of programs - problems are generally broken down, simplified, understood and organised in terms of these structures, a process known as 'abstraction'. This promotes modular programming, which allows code to be reused, better understood and better designed amongst other benefits. Once a procedure or function has been written, it can be called using a single command. This approach isn't perfect, however, because the variables that are used within procedures and functions can always be accidentally changed elsewhere in the program and finding these kinds of bugs can be very difficult and time-consuming!
PASCAL was written as a teaching language, to teach the elements of structured programming and BASIC was used for the same purpose for many years (and still is). COBOL has commands that make it excellent at data processing applications. FORTRAN focuses on scientific and engineering applications and so on. Other procedural languages have also been written. Each one provides the programmer with the instructions and facilities that enable programs for particular application areas to be written.
Declarative languages
We will introduce declarative languages here. There is a chapter later that shows some examples using PROLOG.
A declarative language is very different from a procedural one. It is written in a completely different way. With procedural languages, you have to tell the computer exactly what to do, step-by-step. With declarative languages, you don't. You write a program by stating facts and relationships between facts. Then you ask the program questions. You don't state how to find information, only what information you want. In other words:
-
- You tell the computer facts and how those facts relate to each other.
- Running a program involves you stating a goal and letting the language work out whether the goal can be achieved or not by looking at the facts and their relationships.
- The order of statements is really important in procedural languages such as Pascal. They are not at all important in declarative languages.
- The route through a program needs to be carefully thought through with procedural languages. In declarative languages, the program works through a route of facts as it finds them. If it comes up against a dead-end, it backtracks and tries a different route, until it achieves the goal you set it, or it can't achieve the goal at all.
Declarative languages are suited to artificial intelligence applications. Programs can be written to suggest illnesses from symptoms or to produce probable minerals present in a particular environment, for example.
Object-oriented languages
Good programming using a procedural language will employ a modular approach. There are many good reasons for this. One of them is that procedures and functions can be written which can then be stored in a library and used in other programs. That will save time and money developing future programs. Unfortunately, problems can still arise when you try to use library modules. Variables used in a module, for example, may get changed in an unexpected way by a program, or vice versa. This may cause a bug and if it does, they can be very hard indeed to find.
A different way of programming is the object-oriented approach. In this kind of paradigm, objects are defined. An object is a real-world thing. It might be a car, a person or a command button in Visual Basic. The programmer, before writing the program, asks:
-
- What objects exist in the problem area?
- What events can happen to those objects?
- What should happen, the methods, when a particular event occurs?
The data needed to define an object, the things that can happen to that object (events) and the actions that need to take place when a particular event occurs (methods) are bound together (called encapsulation). Once an object has been encapsulated, it cannot fail as a result of the actions of other parts of the program, as procedural programs can.
OOP is conceptually much closer to the real world than procedural programming. It is much easier to think about what objects there are in a program and what might happen to an object than to try to break down a problem into modules and then write down a sequence of instructions that work through the modules. A later chapter deals with the concepts behind OOP in much more detail.
Functional languages
Functional paradigms originate from a pure Maths discipline and the theory of functions, as opposed to imperative languages, which are rooted in the design of digital computers and the Von Neumann computer. All computations are carried out by calling functions and each function can be evaluated as an expression.
You may want to investigate functional languages. You could start here.
Mark-up languages
HTML (HyperText Markup Language) and CSS (Cascading Style Sheets) are two of the main tools for designing and and displaying websites and web pages. If they are combined with a client-side processing language like JavaScript to add dynamic and interactive content, then websites can be built that are clear, memorable, useful, informative, accessible, organised and maintainable. We have summarised the key points here but there is another article in this section that deals with HTML in more detail.
HTML (HyperText Markup Language)
A typical web page might contain text, pictures, animation, movies and sound and collectively, they are known as multimedia. HTML is a computer language whose purpose is to create multimedia pages on a computer. The first page of a web site is called the 'homepage'. Its file name is usually always either index.html or index.htm and this is the special file a server looks for first when you request a web page. HTML is a mark-up language. It is written as a script - a sequence of instructions called tags that tell a web browser how to display something. It is not a programming language as such - it doesn't use programming constructs such as SEQUENCE, SELECTION and ITERATION. However, it is possible to 'embed' a program written in e.g. JavaScript into an HTML page to add some interactive capabilities or add dynamic data.
CSS (Cascading Style Sheets)
The content of a web page and the instructions that tell the browser how to layout the content are divorced in well written websites. This is achieved by writing two files for a web page. The first file, called a Cascading Style Sheet, or CSS file, holds the layout instructions that tells a web browser how to layout any information on the web page. It deals with actual layout, the fonts to use, the size of the fonts, the colours to use, how to lay out tables, pictures and so on. The second file is the HTML file. This file contains only the content you want to display on a web page. You can set up many different 'style sheets' and use them at any time for any of your web pages. For example, you might have some tags in a style sheet that say "Display all headings in size 1, underlined and red". If this style sheet is applied to a web page then whenever it comes across a piece of information that's a title, it formats it according to the style sheet’s instructions. Style sheets in HTML are very useful. Imagine, for example, you have written 100 pages of information, each with a green page background. If you changed your mind and wanted a blue background, you would only need to change one page, the style sheet, instead of 100 individual pages!
Visual programming languages
Any programming language that allows you to create programs by selecting visual blocks and building programs by combining them can be called a visual programming language. Most students these days have used a visual programming language. These include:
-
- Scratch, to make powerful games
- App Inventor, to write mobile phone apps
- Logicator, to write control programs
These types of languages are very accessible so make them ideal to use in educational settings and to build up skills towards more text-based languages. Because they use graphical elements, it is much harder to get the syntax incorrect (the syntax means the rules of a programming language, which will cause an error if broken).