Programming paradigms

What is a programming paradigm?

A programming paradigm refers to a style of programming. There are 4 main paradigms:
- Procedural
- Object oriented
- Declarative
- Functional
Some languages are written for one specific paradigm but many these days are multi-paradigm languages.

Procedural

Sometimes called procedural oriented programming is where we define a series of instructions that tell the computer what to do with the input it receives to solve the problem. There are a number of languages that can be used for procedural programming including Python, C and Basic.



The structured approach

Procedural programming uses the structured approach. The structured approach refers to combining the 3 basic programming constructs of sequence, selection and iteration to make complex programs.



The structured approach also encourages programmers to break the main program into smaller more manageable subroutines. This splitting up of a program can be shown in a hierarchy chart like the one below.



Structured programming provides a number of advantages:
1. Large programs are split into smaller subprograms that are easier to design
2. Subprograms can be designed by different programmers
3. Subprograms can be tested individually
4. Common subprograms can be kept in libraries and easily imported into other projects

Object oriented programming

Object oriented programming, often referred to as OOP, is a paradigm where problems are defined in terms of the objects they represent. If we consider for instance the objects required to create a game of tic-tac-toe we might have objects such as:
* Gameboard
* Location
* Player
* ComputerPlayer

In an OOP program there will be a number of objects that interact with each other. Each object is responsible for its own data known as its attributes e.g. the Player object in our tic-tac-toe game may have attributes such as name, number of games, number of wins, choice of marker. The values of an objects attributes can only be affected by the methods of the object e.g. To add 1 to the number of games after one is played you would need to use the increment games method of the Player object.

Object oriented languages include Python, C++ and PHP.



Declarative

This is languages where you specify the problem to be solved and the language implementation solves that in the most efficient way. This includes languages such as SQL and Prolog.



Functional

In functional languages functions are the primary building blocks of the language. Programs are comprised of functions that accept input as arguments and return an output. Functions may both accept other functions as inputs and or generate them as outputs.

Data stored in a functional program is immutable, meaning it cannot be changed. The programs are therefore said to be stateless. As values can't be changed functions are said to have no side effects. This means functions have what is called referential integrity meaning they can be called with the same inputs again and again and will always return the same result.

Haskell is a fully functional programming language. Other languages such as Python and C# incorporate aspects of functional programming.



Knowledge check


Questions:
Correct:

Question text


© All materials created by and copyright S.Goff