Have you ever written a program that grew so long it ran to hundreds or thousands of lines? Finding a bug or adding a new feature in that turns into a headache.
To keep our code tidier and easier to manage, we need modularization. The trick is to split a large program into small pieces called subprograms. In programming, we know these subprograms as functions and procedures.
Both concepts are used constantly to group lines of code that share a specific job. Let's look at the difference, and why you really should use them! 🚀
Key takeaways
- A function always returns a value to whoever called it.
- A procedure performs a specific task without returning any value.
- Both keep code modular, easier to debug, and free of repetition (DRY).
Why Use Functions and Procedures?
There are plenty of benefits waiting for you. First, debugging gets much easier. When there is an error in one feature, you only need to check the function or procedure block involved, not dig through the whole program.
Second, reusability. The code you already wrote can be called again and again in different parts of the program. That keeps you from writing identical code many times over (the Don't Repeat Yourself concept, or DRY).
Third, your code looks far more professional and has high readability. Other people, and you six months from now, follow the flow of the program more easily.
Getting to Know Procedures
A procedure is a set of instructions for performing a specific action. Its defining trait is that it does not return a value (no return value) to the main program.
So a procedure gets a task, does it to the end, and that is it. No computed result is handed back to the caller. In modern languages like C or C++, procedures are usually written with the void keyword.
An example in C:
void cetak_struk(int total) {
printf("Total: %d\n", total);
}
Notice there is no return inside. The procedure receives data, does something, and finishes; its work never goes back to whoever called it.
Getting to Know Functions
A function behaves a little differently. A function is a block of code that processes data and, at the end, returns a value (return value).
That computed value gets handed back to the main program. The return value is usually captured into a variable first, then used in the next step.
In one program, you can use both together:
def total(belanja):
return sum(belanja)
def cetak_struk(total_bayar):
print("Total:", total_bayar)
cetak_struk(total([12000, 8000, 15000]))
total() returns a number, and that number goes straight in as the argument to cetak_struk() on the last line. The program prints Total: 35000.
Functions vs Procedures
The most fundamental difference is only the return value. A procedure performs an action with no strings attached, meaning it gives nothing back. A function, after processing data, hands you an output in the form of a definite value.
Usage differs too. A procedure is usually called directly, as one self-contained instruction. A function is often called inside an expression or an assignment operation (storing a value into a variable).
When to Use a Function, When a Procedure?
Which one to use comes back to what you need. Use a procedure when you only need to perform an action. Printing text to the screen, saving data to a database, or changing a global variable all fit there.
But when you need a math calculation, text processing, or a data conversion whose result the main program will use again, that is when you reach for a function. Easy to follow, right? ✨