What Is Functional Programming?
If you understood the previous lesson on Object-Oriented Programming (OOP), you’ll find Functional Programming (FP) much easier to understand.
In OOP, we build a Class and then create one or more Objects from it.
Functional Programming takes a simpler approach.
Instead of focusing on classes and objects, it focuses on functions that receive data, process it, and return a result.
In its simplest form, the idea can be expressed as:
Input → Processing → Output
This is the core concept behind Functional Programming.
Example
Let’s write a program that adds two numbers:
function sumTwoNumbers(number1, number2) { return number1 + number2;}
console.log(sumTwoNumbers(10, 20));How Does It Work?
First, we use the function keyword to define a function named sumTwoNumbers.
The function receives two inputs: number1 and number2.
It processes those values by adding them together.
The return keyword sends the result back to whoever called the function.
Finally, we call the function with the values 10 and 20, and it returns 30.
Notice the pattern:
- Receive input.
- Process the data.
- Return the output.
That simple flow is the foundation of Functional Programming.
Functional Programming Is More Than Functions
This example might make Functional Programming seem like “just writing functions,” but it is much more than that.
It is a programming paradigm built around principles such as writing small reusable functions, minimizing side effects, and making code easier to test and maintain.
For now, however, the most important idea to remember is that functions transform input into output.
Key Takeaways
- Functional Programming is a programming paradigm.
- It focuses on using functions to process data.
- A function receives inputs, processes them, and returns outputs.
- This lesson introduces the core idea; the advanced principles of Functional Programming will be covered later.