Programming paradigms
A programming paradigm is an approach to solve problems using some programming language or also we can say it is a method to solve a problem using tools and techniques that are available to us following some approach. There are lots of programming languages that are known but all of them need to follow some strategy when they are implemented and this methodology/strategy is paradigms. Apart from varieties of programming languages, there are lots of paradigms to fulfil each and every demand.
Programming paradigm consists of two types:
- Imperative Programming as the name suggests is a type of programming paradigm that describes how the program executes. Developers are more concerned with how to get an answer step by step. It comprises the sequence of command imperatives. In this, the order of execution is very important and uses both mutable and immutable data. Fortran, Java , c++, c programming languages are examples of imperative programming.
- Declarative Programming as the name suggests is a type of programming paradigm that describes what programs to be executed. Developers are more concerned with the answer that is received. It declares what kind of results we want and leave programming language aside focusing on simply figuring out how to produce them. In simple words, it mainly focuses on end result. It expresses the logic of computation. Miranda, Erlang, Haskell, Prolog are a few popular examples of declarative programming.
Let us go on a whirlwind tour of 4 different sub programming paradigms — Procedural, Object-Oriented, Functional and Logical.
1. Procedural Programming
Procedural programming can also be referred to as imperative programming. It is a programming paradigm based upon the concept of procedure calls, in which statements are structured into procedures (also known as subroutines or functions). They are a list of instructions to tell the computer what to do step by step, Procedural programming languages are known as top-down languages. Most of the early programming languages are all procedural.
Examples of Procedural languages:
FORTRAN, COBOL, ALGOL, BASIC, C and Pascal.
2. Object-Oriented Programming(OO):
In this framework, all real-world entities are represented by Classes. Objects are instances of classes so each object encapsulates a state and behaviour. State implies the fields, attributes of the object and behaviour is what you do with the state of the object and they are the methods. Objects interact with each other by passing messages.
Features of OO:
- Encapsulation — This is a fundamental feature of Object-Oriented Programming. Here you hide unnecessary details in classes and deliver a simple and clear interface for working. It describes the idea of bundling data and methods that work on that data within one unit. This concept is also often used to hide the internal representation, or state, of an object from the outside
- Inheritance — Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can derive a class from another class for a hierarchy of classes that share a set of attributes and methods. It explains how the class hierarchies develop code readability and support to the reuse of functionality.
- Data Abstraction — to Data abstraction is the reduction of a particular body of data to a simplified representation of the whole. Data abstraction is usually the first step in database design.
- Polymorphism — Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or object to take on multiple forms.
Examples of OOP languages:
Java, C++, C#, Python, JavaScript, Ruby, Perl, Objective-C, Dart, Swift
3. Logical Programming
Logical programming is a computer programming paradigm that has its foundations in mathematical logic in which program statements express facts and rules about problems within a system.
Rules are written as logical clauses with a head and a body. They also follow a declarative rather than an imperative approach. However, what does that mean?
To understand how a problem can be solved in logical programming, you need to know about the building blocks − Facts and Rules −
Let us understand the difference between Imperative and declarative programming.
Imagine you walk into your favourite coffee place and you would like to order some coffee.
The imperative approach will be:
- Enter the coffee shop
- Queue in the line and wait for the barista asking you for your order
- Order
- Yes, for takeaway, please
- Pay
- Present your loyalty card to collect points
- Take your order and walk away
The declarative approach:
- A large latte for takeaway, please
So rather than providing a step by step instruction (imperative), you tell the system what you need and let it try to come up with a solution (declarative).
Prolog follows the Logical paradigm and is probably the most famous language in the logical programming family.
Prolog has been enormously influential in the domains of theorem proving, expert systems, natural language processing and in the field of artificial intelligence (notably IBM’s Watson2) in general
Prolog, like SQL, has two main aspects, one to express the data and another to query it. The basic constructs of logical programming, terms, and statements, are inherited from logic. There are three basic statements:
- Facts are fundamental assertions about the problem domain (e.g. “Socrates is a man”)
- Rules are inferences about facts in the domain (e.g. “All men are mortal.”)
- Queries are questions about that domain (e.g. “Is Socrates mortal?”)
Features of Logical Programming
- Logical programming can be used to express knowledge in a way that does not depend on the
implementation, making programs more flexible, compressed and understandable. - It enables knowledge to be separated from use, i.e. the machine architecture can be changed
without changing programs or their underlying code. - It can be altered and extended in natural ways to support special forms of knowledge, such
as meta-level of higher-order knowledge. - It can be used in non-computational disciplines relying on reasoning and precise means of
expression.
Examples of Logical languages:
Prolog
4. Functional Programming
Functional programming is a programming paradigm where you have a style of building the structure and elements of computer programs. Here you treat computation as an evaluation of mathematical functions and you avoid changing-state and mutable data.
Functional programming consists only of PURE functions. So, what do you understand by Pure functions?
Pure functions are those which take an argument list as an input and whose output is a return value. Now you may feel that all functions are pure as any function takes in values and returns a value.
For example, if a function relies on the global variable or class member’s data, then it is not pure. And in such cases, the return value of that function is not entirely dependent on the list of arguments received as input and can also have side effects. So, what do you understand by the term side effect? A side effect is a change in the state of an application that is observable outside the called function other than its return value. For example: Modifying any external variable or object property such as a global variable, or a variable in the parent function scope chain.
Features of Functional Paradigm
Pure functions as seen above, if the input is an array, the output will be a new array and the input array will not be modified. So in case of pure functions, the output depends only on the input.
Here’s a function in the language Scala that takes values and returns their sum.
scala> def add(a:Int,b:Int) = a + b
add:(a:Int, b:Int) Int
The add function caused no side-effects. It did not alter the input values provided, it used another pure function, the + operator, and returned the sum of the values as the result of the call. The add function is a pure function.
Recursion a recursive function is a function that calls itself during its execution. This enables the function to repeat itself several times, the result being outputted at the end of each iteration. Below is an example of a recursive function.
Function Count (integer N)
if(N <= 0) return “Must be a Positive Integer”;
if(N > 9) return “Counting Completed”;
Else return Count (N+1);
end function
The function Count() above uses recursion to count from any number between 1 and 9, to the number 10. For example, Count(1) would return 2,3,4,5,6,7,8,9,10. Count(7) would return 8,9,10.
You will note that recursive functions are common in computer science because they allow you to write efficient programs using a minimal amount of code. The downside is that they can cause infinite loops and other unexpected results if not written properly. For example, in the example above, the function is terminated if the number is 0 or less or greater than 9. If proper cases are not included in the function to stop the execution, the recursion will repeat forever, causing the program to crash, or worse yet, hang the entire computer system.
Referential transparency an expression is said to be referentially transparent if it can be replaced with its corresponding value without changing the program’s behaviour. As a result, evaluating a referentially transparent function gives the same value for fixed arguments. In functional programming, only referentially transparent functions are considered. They are a lot easier to read and understand.
Int add(int a, int b){
return a + b
}
Int mult(int a, int b){
return a * b;
}
Int x = add(2, mult(3, 4));
In this example, the mult method is referentially transparent because any call to it may be replaced with the corresponding return value. This may be observed by replacing mult(3, 4) with 12:
Int x = add(2, 12)
In the same way, add(2, 12) may be replaced with the corresponding return value, 14:
intx = 14;
Functions are First-Class and can be Higher-Order- A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable. Higher-order functions are functions that take at least one first-class function as a parameter.
Variables are Immutable- In functional programming you cannot modify a variable after it has been initialised. You can create new variables and this helps to maintain state throughout the runtime of a program.
Strings are immutable objects.
Var name = “Susie”
Var uppd = name.toUpperCase()
*name will have the value — Susie
*uppd will have the value — SUSIE
Examples of Logical languages:
Haskell, JavaScript, Scala, Erlang, Lisp, ML, Clojure, OCaml, Common Lisp, Racket.