Event programming, also known as event-driven programming, is a programming paradigm that focuses on handling events triggered by user actions or system events. The program typically waits for events to occur and then responds to them. Event programming is commonly used in graphical user interfaces (GUIs) and web development.
Functional programming is a programming paradigm that emphasizes the use of mathematical functions to solve problems. It treats computation as the evaluation of functions and avoids changing state and mutable data. Functional programming languages often support higher-order functions, immutability, and recursion.
Object-oriented programming (OOP) is a programming paradigm that organizes software design around objects, which are instances of classes. Objects encapsulate data and behavior, and interact with each other through messages and method calls. OOP focuses on modularity, reusability, and extensibility.
Procedural programming is a programming paradigm that follows a linear top-down approach, where the program is structured into procedures (also known as functions or subroutines). Procedures contain a series of steps or instructions that are executed sequentially. Procedural programming is commonly used in scripting languages and embedded systems.
Here is an example code snippet for each of the programming paradigms:
Event programming (JavaScript):
JavaScript:
// Event handling using an event listener
document.getElementById("myButton").addEventListener("click", function() {
console.log("Button clicked!");
});
Functional programming (Haskell):
Code:
-- Function to calculate factorial using recursion
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
Object-oriented programming (Java):
Java:
// Class definition for a Rectangle object
public class Rectangle {
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int calculateArea() {
return width * height;
}
}
Procedural programming (C):
C:
#include <stdio.h>
// Function to calculate sum of two numbers
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("Sum: %d\n", result);
return 0;
}
These examples illustrate the main differences between event programming, functional programming, object-oriented programming, and procedural programming in terms of their syntax, structure, and approach to problem-solving.