Pure Functions
Subject: Functional Programming (CSC 371)
Introduction
When writing programs, one of the biggest challenges is managing changes in data. As programs grow larger, it becomes difficult to track which part of the code changed a variable, modified a list, or produced an unexpected result. Functional programming addresses this problem through two important concepts:
Pure Functions and Immutability.
These concepts help programmers write code that is predictable, easier to test, easier to debug, and safer to use in large applications. Many modern technologies and programming frameworks rely heavily on these ideas because they reduce unexpected behavior and make programs more reliable.
Understanding Pure Functions
A pure function is a function whose output depends only on its input values and which does not alter any external state.
In simpler terms, a pure function behaves like a mathematical formula. If you provide the same inputs, you will always get the same output, regardless of when or where the function is executed.
For example, in mathematics:
f(x)=x+5If x=10, the answer will always be 15. The result does not depend on the weather, time of day, or any external variable.
Similarly, a pure function in programming should produce a consistent result every time it is called with the same arguments.
Characteristics of a Pure Function
A function is considered pure if it satisfies the following conditions:
1.
Deterministic Behavior: The function always returns the same result for the same inputs.
Consider the function below (python):
def multiply(a, b):
return a * b
If we execute:
multiply(4, 5)
the result will always be: 20
Whether the function is called today, tomorrow, or next year, the output remains the same.
2.
No Side Effects: a pure function does not modify anything outside its own scope.
It does not:
- Change global variables
- Modify external files
- Update databases
- Alter lists passed to it
- Print messages to the screen
- Depend on user input during execution
The function only receives data, processes it, and returns a result.
Real-Life Analogy
Imagine a vending machine, you insert ₦500 and select a bottle of water worth ₦300.
The machine returns:
Water
₦200 change
Every time you perform the same action, you receive the same result. This is similar to a pure function because the output depends solely on the input.
Now imagine a machine that sometimes gives water, sometimes juice, and sometimes nothing despite receiving the same amount of money. Such behavior would be unpredictable and similar to an impure function.
def area(length, width):
return length * width
print(area(6, 4))
output is 24
This function behaves exactly like a mathematical formula and is therefore pure.
What Are Side Effects?
A side effect occurs whenever a function changes something outside itself or interacts with the external environment.
Side effects make programs harder to understand because the behavior of the function is no longer determined solely by its inputs.
Common Types of Side Effects1. Modifying a Global Variable:
count = 0
def increase():
global count
count += 1
The function changes a variable outside its scope.
2. Printing to the Screen
def greet(name):
print("Hello", name)
3. Writing to a File
def save_data():
with open("data.txt", "w") as file:
file.write("Hello")
The function changes a file on the computer.
Why Are Pure Functions Important?
Pure functions provide several advantages.
1.
Easier Testing: since the output depends only on the inputs, testing becomes straightforward. The test will always produce the same result.
2.
Easier Debugging: when a pure function produces an incorrect result, the problem is usually inside the function itself. There is no need to investigate external variables or hidden state changes.
3.
Reusability: Pure functions can be used in different programs without modification because they do not depend on external conditions.
4.
Predictability: Developers can understand the behavior of pure functions simply by looking at their inputs and outputs.
By:
Vision University
Login to comment or ask question on this topic
Previous Topic