Programming paradigm
Programming paradigm
Programming paradigm
Recursion
There are no “for” or “while” loop in functional languages. Iteration
in functional languages is implemented through recursion.
Recursive functions repeatedly call themselves, until it reaches the
base case.
Variables are Immutable
In functional programming, we can’t modify a variable after it’s been
initialized. We can create new variables – but we can’t modify existing
variables, and this really helps to maintain state throughout the
runtime of a program. Once we create a variable and set its value, we
can have full confidence knowing that the value of that variable will
never change.
Referential Transparency
Referential transparency is a property of a function that allows it to be replaced
by its equivalent output. In simpler terms, if you call the function a second time
with the same arguments, you’re guaranteed to get the same returning value.
Example
Let’s consider the following function, which computes the sum of 2 numbers:
algorithm add(a, b):
// This function is referentially opaque.
int a, b
read a, b
return a + b
The function is referentially opaque because the parameters defined in its signature aren’t all
of the resources the function uses.
The a and b variables are read from standard input inside the function body, so two
function calls with the same arguments may produce different results.
In order to make the function referentially transparent, we need to extract the input part
from the function:
algorithm add(a, b):
return a + b
algorithm main():
// This function handles user input and calls the add function to compute the sum of two
numbers.int a, b
read a, b
int s = add(a, b)
print s
return 0Copy
Note that this doesn’t prevent us from interacting with user input, we’re still able to read
the values for a and b. Thus, in the code snippet, the add function is transparent, while
the main function isn’t.
Advantages of Functional Programming
Pure functions are easier to understand because they don’t change any states and
depend only on the input given to them. Whatever output they produce is the return
value they give. Their function signature gives all the information about them i.e.
their return type and their arguments.
The ability of functional programming languages to treat functions as values and
pass them to functions as parameters make the code more readable and easily
understandable.
Testing and debugging is easier. Since pure functions take only arguments and
produce output, they don’t produce any changes don’t take input or produce some
hidden output. They use immutable values, so it becomes easier to check some
problems in programs written uses pure functions.
It is used to implement concurrency/parallelism because pure functions don’t
change variables or any other data outside of it.
Object oriented programming paradigm
OOP is a programming paradigm used for software development. Its basis is modeling
objects and relationships in real world. OOP paradigm of our software projects; It
makes it more modular, readable, understandable and easy to maintain
4- Inheritance:
Inheritance is defined as a class inheriting features and behaviors from another
base class. Inheritance represents the parent-child relationship between classes.
Benefits of inheritance:
1 — Increases code reusability and prevents code duplication.
2 — We create general classes by bringing similar classes together through
inheritance. In this way, it offers a high-level and abstract perspective.
3 — Derived classes can extend the features of base classes and add features and
behaviors. This allows the code to be more flexible and customizable.
4 — Thanks to the hierarchical structure of inheritance, complex systems become
more orderly.
5- Polymorphism:
First of all, in order to talk about polymorphism, it is necessary to consider the
concept of “override”. Override is when the subclass changes a certain
behavior or function of the parent class according to its own needs.
Polymorphism also supports and maintains this. It redesigns the content of a
method inherited from the superclass according to its own needs and logic.
Benefits of polymorphism:
1 — Enables objects in the same class hierarchy to behave in different
ways using the same interface.
2 — Provides flexibility by customizing different objects.
3 — The reusability of the code increases.
Differences between Procedural and Object Oriented Programming
Procedural Programming
Procedural Programming can be defined as a programming model
which is derived from structured programming, based upon the
concept of calling procedure. Procedures, also known as routines,
subroutines or functions, simply consist of a series of computational
steps to be carried out. During a program’s execution, any given
procedure might be called at any point, including by other procedures
or itself.
Languages used in Procedural Programming: FORTRAN, ALGOL,
COBOL, BASIC, Pascal and C.
Object-Oriented Programming
Object-oriented programming can be defined as a programming model which is
based upon the concept of objects. Objects contain data in the form of
attributes and code in the form of methods. In object-oriented programming,
computer programs are designed using the concept of objects that interact
with the real world.
Java, C++, C#, Python,
PHP, JavaScript, Ruby, Perl,
Objective-C, Dart, Swift, Scala.
Procedural Oriented Programming Object-Oriented Programming
In procedural programming, the program is In object-oriented programming, the program is divided into
divided into small parts called functions. small parts called objects.
There is no access specifier in procedural Object-oriented programming has access specifiers like private,
programming. public, protected, etc.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any Object-oriented programming provides data hiding so it is more
proper way of hiding data so it is less secure. secure.
Procedural programming is based on the unreal Object-oriented programming is based on the real
world. world.
Procedural programming is used for designing Object-oriented programming is used for designing
medium-sized programs. large and complex programs.
Procedural programming uses the concept of Object-oriented programming uses the concept of
procedure abstraction. data abstraction.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
Logic Programming
ogical programming is a paradigm in which logical circuits are used to represent the problems
with the help of facts and rules. Knowledge is represented with the help of logic. This
knowledge can be modified with the help of inference. This paradigm helps in knowing the
ways to accomplish certain goals.
Logical language is based on another paradigm where sentences are evaluated by rules
based on pure logic.
A Logical language has features that allow the programmer to write statements expressing
that, if some set of facts are true, then another set of facts are true.
this is the trend that languages are heading towards. Java and Python started as object
oriented, but have had features from functional programming (lambdas, map, filter, reduce)
for a while now. Prolog is mixed imperative and logical. Scala was designed to be mixed
paradigm from day one.
Functional Programming Logical Programming
It uses one or more functions in a program. It uses predicates which do not have a return value.
Functions have to be written and executed in this type Statements are used in this type of programming. These
of programming. statements represent the problems in the form of logic.
The languages that use functional programming The programming languages that use logic programming
are Clojure, OCaml, Wolfram language, etc. are Absys, Cycl, Alice, etc.
Code redundancy is reduced and it can also be used to It is a data-driven programming which is used to provide
solve complex problems. knowledge.