Hi everyone! Today we are going to see about Programming Paradigm.
What is a Programming Paradigm?
A programming paradigm is just a style or method of writing code.
It’s like different ways to solve a problem using a programming language.
1. Procedural Programming
This paradigm emphasizes on procedure in terms of under lying machine model. There is no difference in between procedural and imperative approach. It has the ability to reuse the code and it was boon at that time when it was in use because of its reusability.
Example:js
// Prompt the user for input
let num = prompt("Enter any Number: ");
// Initialize the factorial value to 1
let fact = 1;
// Calculate the factorial of the number
for (let i = 1; i <= num; i++) {
fact = fact * i;
}
// Print the factorial of the number
console.log("Factorial of " + num + " is: " + fact);
2. Object-Oriented Programming (OOP)
The program is written as a collection of classes and object which are meant for communication. The smallest and basic entity is object and all kind of computation is performed on the objects only. More emphasis is on data rather procedure. It can handle almost all kind of real life problems which are today in scenario.
Advantages:
- Data security
- Inheritance
- Code reusability
- Flexible and abstraction is also present
Example:js
class Signup {
constructor(userid, name, emailid, sex, mob) {
this.userid = userid;
this.name = name;
this.emailid = emailid;
this.sex = sex;
this.mob = mob;
}
create(userid, name, emailid, sex, mob) {
console.log("Welcome to GeeksforGeeks\nLets create your account\n");
this.userid = 132;
this.name = "Radha";
this.emailid = "radha.89@gmail.com";
this.sex = 'F';
this.mob = 900558981;
console.log("your account has been created");
}
}
console.log("GfG!");
let s1 = new Signup();
s1.create(22, "riya", "riya2@gmail.com", 'F', 89002);
// This code is contributed by akshatve2zi2
3. Functional Programming
The functional programming paradigms has its roots in mathematics and it is language independent. The key principle of this paradigms is the execution of series of mathematical functions.
Example:js
// Functional style
const square = (x) => x * x;
console.log("Square is:", square(5));
Note: In functional programming, we use pure functions and avoid changing variables.
4. Declarative Programming
It is divided as Logic, Functional, Database. In computer science the declarative programming is a style of building programs that expresses logic of computation without talking about its control flow. It often considers programs as theories of some logic.It may simplify writing parallel programs.
Example:js
// React (Declarative UI)
function App() {
const num = 5;
return <h1>Square is {num * num}</h1>;
}
In declarative programming, we describe what we want, not how to do it.
5. Logic Programming
It can be termed as abstract model of computation. It would solve logical problems like puzzles, series etc. In logic programming we have a knowledge base which we know before and along with the question and knowledge base which is given to machine, it produces result.
Example:js
// Simulated logic style using conditions
function square(x, y) {
if (y === x * x) {
return true;
}
return false;
}
console.log(square(5, 25)); // true
console.log(square(5, 20)); // false
True logic programming (like in Prolog) uses rules and lets the system find the answer.
So that's it guys. I hope I gave a clear explanation about these topic. So let me know if anything I gave wrong here. Thank you for reading my blog. Will se in my next Blog.
Resource:https://www.geeksforgeeks.org/system-design/introduction-of-programming-paradigms/
Top comments (1)
Declarative + Functional - OOP