What if you could write a piece of code once and use it everywhere without repeating yourself?
Why Function declaration and definition? - Purpose & Use Cases
Imagine you are writing a long program in C where you need to repeat the same task multiple times, like calculating the area of different rectangles. Without functions, you would have to write the same code again and again in every place you need it.
Writing the same code repeatedly is slow and tiring. It's easy to make mistakes when copying and pasting. If you want to change the calculation, you must find and update every copy, which wastes time and causes bugs.
Functions let you write the code for a task once and give it a name. You declare the function to tell the program about it, and define it to write what it does. Then you just call the function whenever you need it, making your code cleaner and easier to fix.
int main() {
int area1 = 5 * 10;
int area2 = 7 * 3;
// repeated code for area calculation
}int area(int width, int height); // declaration
int main() {
int area1 = area(5, 10);
int area2 = area(7, 3);
return 0;
}
int area(int width, int height) { // definition
return width * height;
}Functions make your programs organized, reusable, and easy to update, unlocking the power to build bigger and better software.
Think of a recipe book: you write the recipe once (function definition), tell your friends about it (declaration), and they can use it anytime without rewriting the steps.
Functions let you reuse code by naming tasks.
Declaration tells the program about the function.
Definition contains the actual code to run.