Is this a function in JavaScript?
function fiveSquared() {
return 5 * 5
}
Technically, yes.
fiveSquared, instead of technically being a function, it doesn't have all the characteristics of a real-world function: reusability. No matter how many times you call fiveSquared(), it will always return 25. If you suddenly need to calculate the square of 6, you're stuck. You would have to write a brand new function called sixSquared just to do 6 * 6. This is the "pain of the century", doing the same work over and over again!
Even though this code is a function, it defeats the fundamental principle of DRY (Don’t Repeat Yourself). A function's true purpose isn't just to hold a calculation; it’s to provide a flexible logic that saves you from writing the same code twice.
The Cure for the "Pain of the Century"
So, how do we fix a function like fiveSquared so it doesn't break the DRY principle? We use Parameters.
Instead of hardcoding the number 5 inside the logic, we create a "placeholder" (an input variable). This allows the function to stay the same, while the numbers we give it can change. Here is the better way to do it:
function calculateSquare(num) {
return num * num;
}
By adding num inside the parentheses, we’ve just saved ourselves a lifetime of work! Now, we don't need to write fiveSquared, sixSquared, or sevenSquared. We have one single function that can handle any number in the world.
When we call the function like this:
calculateSquare(5) returns 25
calculateSquare(6) returns 36
calculateSquare(100) returns 10,000
This is the true power of JavaScript functions. We wrote the logic once, and now we can reuse it forever. We finally moved away from the "pain" of repeating ourselves and started writing smart, scalable code.
Final Conclusion
Writing functions is one of the first big steps in your coding journey. It’s the difference between doing manual labor and building a machine that works for you.
Remember: A great function doesn't just do a task, it provides a flexible logic that you can use over and over again without ever having to rewrite it. By using parameters and avoiding hardcoded values, you move away from the "pain of the century" and toward becoming a clean, efficient developer.
Keep practicing, keep your code DRY, and most importantly, stop repeating yourself!
Test Your Knowledge: The Function Challenge!
- Write a function called multiplyByTen that takes one parameter and returns that number multiplied by 10.
- What happens if you create a function with a parameter but forget to use that parameter inside the logic? Does it still follow the DRY principle?
- Can you write a function that takes two different parameters (like num1 and num2) and adds them together?
What is next?
In this article, we discovered that we can leave a placeholder for data (a parameter). This allows us to pass different numbers into calculateSquare later on, finally making our code reusable.
But what if we could go even deeper?
In the next article, we are going to learn how to leave a placeholder for the operation itself. Just like we passed in a different number, we can actually pass in a different action(operation). Right now, our function is stuck doing multiplication, but what if we could tell it to multiply one day and add the next, without changing the core function?
Get ready, because we're about to learn how to make our logic just as flexible as our data by using something called Higher Order Function (HOF for short).
Top comments (0)