Join WhatsApp

Join Now

Join Telegram

Join Now

Functions in C: Write Once, Call Forever

By Sagar Miraje

Published On:

Follow Us

The most important part of programming in any language is functions. It was a humid Thursday in July when a student walked up to me after class, floppy notebook in hand and a look of pure despair in her eyes. 
“Sir,” she said, “my code is 356 lines. I just wanted to calculate CGPAs. I don’t know where anything is anymore.” 
I smiled and said, “You’re ready.” 
“For what?” she asked. 
“For functions,” I whispered like a wizard revealing the ancient art of spellcasting. 

Because here’s the truth: every programmer eventually hits the wall that overwhelming moment when your codebase turns into a labyrinth of scanf, printf, int a, b, c, d, e, f…, and so many nested ifs that even you can’t remember what they’re checking anymore. I’ve been there. We all have. 

And that’s why I always say: 
When your code becomes chaos, functions bring order. 
They’re not just syntax. They’re salvation. 

The Day I Cried Over 300 Lines of Code 

My first brush with despair came during my own college project a simple bank system in C. Just five operations: create account, deposit, withdraw, check balance, and exit. Easy, right? 

By the time I added user menus, error handling, input validation, and basic file I/O, my main function had mutated into a 300 line monster. I was scrolling endlessly, copy pasting the same 8 line blocks every time a transaction happened. I knew there had to be a better way. Then our professor wrote two magical words on the board: 

void greet() 

I stared. A function? That simple? 

void greet() { 
   printf(“Welcome to the Bank Management System\n”); 

 

He called it in main() like this: 

greet(); 

Just one line in main, and that chunk of greeting logic was offloaded. 
It felt like discovering fire. 

What Are Functions, Really? 

I tell my students: 
A function is like a kitchen recipe. 
If you’re baking a cake ten times, you don’t rewrite the instructions every time. You just say, “Use the chocolate cake recipe.” 

In C, instead of copy pasting your logic everywhere, you wrap it in a function and call it wherever you need. 

int add(int a, int b) { 
   return a + b; 

 

This is a function that takes two ingredients a and b and returns the result of mixing them (i.e., adding them). You call it like: 

int result = add(5, 10); 
 

And that’s it. No duplication. No confusion. No scrolling through spaghetti. 

Why I Now Preach “Write Once, Call Many” 

Every semester, I make students rewrite their long code using functions. At first, they grumble. 
“I’ll just copy this part again, sir.” 
Then I say, “What if your requirement changes?” 
Their eyes widen. 

One semester, a student named Arjun had written a billing system. But the tax calculation formula changed the night before submission. He had used the same 6 line block in 12 different places. 
He stayed up until 3 a.m. updating all twelve. 

His friend Aisha? She had it as: 

float calculateTax(float price) { 
   return price * 0.18; 

 

She just changed 0.18 to 0.21. Done. 
Arjun learned. 
He now preaches functions in his company like a prophet. 

Understanding Function Anatomy (No White Lab Coats Needed) 

Let’s break it down with the most common example I teach in class: 

int add(int x, int y) { 
   return x + y; 

 

Here’s what’s happening: 

  • int Return type: the function will return an integer. 
  • add Function name: how you’ll refer to it. 
  • (int x, int y) Parameters: placeholders for the values you’ll give. 
  • { return x + y; } Body: what the function actually does. 

How do you call it? 

int result = add(10, 20); 
 

Boom result is now 30. 

But here’s the part that blows students’ minds: 
You can call it 100 times with different values, and the logic stays untouched. 
You’ve built a reusable, portable block of logic. 

Real life Metaphor: The Coffee Machine 

If main() is your office, then a function is your coffee machine. 
You don’t brew coffee manually each time boiling water, grinding beans, measuring milk. 

You press a button: 
makeCoffee(“Espresso”); 

Functions are those buttons. 
Want to change the coffee strength? Change the function. The rest of the office (your code) just calls the button. 

Function Declaration vs Definition vs Call The Student Confusion Trio 

One of the most repeated clarifications I give in class is this: 

  1. Declaration / Prototype 
    This tells the compiler what the function looks like (before it’s defined). 

int add(int, int); // No variable names needed 
 

  1. Definition 
    The actual implementation. 

int add(int a, int b) { 
   return a + b; 

 

  1. Calling 
    Using the function. 

int result = add(5, 10); 
 

Common mistake? 
Students often write: 

result = int add(5, 10); // Wrong! 
 

You never write the return type when calling a function. It’s like ordering food you just say, “One pizza,” not “Deliver me a pizza that returns carbs.” 

Parameters vs Arguments: The Great Debate 

“Sir, what’s the difference between arguments and parameters?” 

I draw this on the board every time: 

// Declaration 
int add(int a, int b);   // a, b are parameters (formal) 
 
// Call 
add(x, y);               // x, y are arguments (actual) 
 

Parameters = variables inside the function definition. 
Arguments = values you pass from outside. 

Think of a cooking class. 
The recipe (function) says: “Add 2 cups of [ingredient1] and 1 cup of [ingredient2].” 
When you cook it, you pass: sugar and flour. 
Those are your arguments. 

When Functions Are Mandatory 

There are two moments in a student’s life when they embrace functions: 

  1. When they write their first menu driven program. 
  1. When they get tired of copy pasting scanf for the 9th time. 

I force them to write calculator programs with: 

int add(), subtract(), multiply(), divide(); 
 

Each operation goes into a function. The main() becomes a neat control center. 

switch(choice) { 
   case 1: result = add(a, b); break; 
   case 2: result = subtract(a, b); break; 
   // … 

 

Suddenly, what was a 150 line mess becomes a 50 line masterpiece. 

Returning Nothing: void Explained Through Story 

I once asked a student, “What do you think void means?” 

He said, “It’s like… empty? Like it does something but doesn’t give anything back.” 

Perfect. 

void functions perform actions but don’t return values: 

void greetUser() { 
   printf(“Welcome to the system.\n”); 

 

You use them when output is enough, no calculation needed. 

Another favorite example I give: 

void printLine() { 
   printf(” \n”); 

 

Call it wherever you want a nice separator. 
Reusable. Clean. Modular. 

How Control Flows: Behind the Curtain 

Another concept I drill into students is how the program’s control moves. 

int main() { 
   int x = 5, y = 10; 
   int result = add(x, y); 
   printf(“Sum is %d”, result); 

 

Behind the scenes: 

  1. Main starts. 
  1. Encounters add(x, y). 
  1. Control jumps to the add function. 
  1. Executes return a + b. 
  1. Control returns back to main. 
  1. The returned value gets stored in result. 

Simple once you get it, but magical the first time it works. 

Mistakes I See All the Time (And Smile When I Fix) 

  1. Putting a semicolon after function header in definition: 

int add(int a, int b); // Wrong if defining, not declaring 
 

  1. Trying to call a function before declaring or defining it. 

Always declare (or define) before you call unless you want the compiler to throw a tantrum. 

  1. Returning nothing from int functions. 

int add(int a, int b) { 
   a + b; // Oops, forgot return 

 

That’s like writing a letter and not posting it. Result never leaves the room. 

  1. Mixing up parameter types. 

int add(float a, int b); // Called with (int, int)? May give warning or wrong results 
 

  1. Thinking function variables persist. 

Each call gets fresh variables. Local variables die when the function ends. 
Want something to live? Use static or return it. 

The Final Metaphor I Leave My Students With 

I end every “functions” session with this: 

“Imagine writing a novel without paragraphs, chapters, or even punctuation. 
Now imagine writing it with clear sections, titles, and bookmarks. 
That’s what functions do to your code.” 

Can a function return multiple values in C? 

No, but you can use pointers or structures to simulate that. 

What if two functions have the same name? 

C doesn’t allow function overloading. Function names must be unique. 

Can I define a function inside another? 

No, C doesn’t support nested function definitions. 

What happens if I forget to declare a function before calling? 

In older C, it might assume return type is int. In modern C, it throws an error

I am Sagar Miraje, a Computer Science graduate with a Bachelor of Technology and 7 years of experience as a C language programming developer. I specialize in writing efficient, low-level code for systems programming, embedded applications, and performance-critical software. My strength lies in optimizing memory usage, handling pointers, and working close to the hardware to deliver fast and reliable solutions. Over the years, I’ve contributed to core system modules, debugged complex runtime issues, and collaborated with cross-functional engineering teams. I’m passionate about problem-solving and always eager to push the limits of what C programming can achieve.

Leave a Comment