C Functions
Updated 8 min read Aimerscrew
As your programs grow, writing everything inside main quickly turns into a long, tangled wall of code. C functions fix that: they let you give a block of code a name and run it whenever you need it, just by writing that name. In this lesson, you'll learn what a function is, how to define and call one, what actually happens when a function runs, and why C cares about the order in which you write them.
🌍 Real-life example
Think of a coffee machine at the office. You don't grind beans, heat water and pour by hand every morning. You press one button labeled Coffee, the machine runs its steps, and then you get on with your day. A function is that button: the steps are written once, inside the machine, and anyone can "press" it by calling its name.
What is a function in C?
A function is a named block of code that performs one specific task. You write the code once (this is called defining the function) and then run it as many times as you want (this is called calling the function).
You have been calling functions since your very first program. printf is a function someone else wrote and stored in the standard library. Now you'll write your own.
Why use functions?
- No repetition: write a task once, call it from many places. If the task changes, you fix it in one spot.
- Readability: a call like
print_receipt();tells the reader what happens without making them read every line. - Smaller problems: a big program becomes a set of small, focused pieces that are easier to write and test.
- Teamwork: different people can work on different functions at the same time.
How do you define a function in C?
A function definition has four parts:
return_type function_name(parameters) {
/* body: the statements that do the work */
}
| Part | Meaning | Example |
|---|---|---|
| Return type | The type of value the function gives back. void means "nothing". | void, int, double |
| Name | What you call it. Same naming rules as variables. | make_coffee |
| Parameters | Inputs, inside parentheses. (void) means "no inputs". | (void) |
| Body | The statements inside { } that run on each call. | { printf(...); } |
In this lesson, all functions take no inputs and return nothing. You'll add inputs in function parameters and outputs in return values.
Defining and calling your first function
Here's the coffee machine as a C function. To call it, write its name followed by parentheses and a semicolon: make_coffee();
#include <stdio.h>
void make_coffee(void) {
printf("Grinding beans...\n");
printf("Pouring coffee...\n");
}
int main(void) {
printf("Good morning!\n");
make_coffee();
printf("Back to work.\n");
return 0;
}
Good morning!
Grinding beans...
Pouring coffee...
Back to work.
Notice that the program still starts in main, even though make_coffee is written first. Defining a function does not run it. Its body only runs when something calls it.
What happens when you call a function?
A function call is like a detour. When C reaches make_coffee();, it pauses main, jumps to the first line of make_coffee, runs its body from top to bottom, and at the closing brace returns to the exact spot it left, continuing with the next line of main.
The part of the program that makes the call is called the caller. Here, main is the caller and make_coffee is the function being called.
Calling a function many times
The real payoff comes when you reuse a function. Functions can also call other functions. This greeting card printer uses print_border twice inside print_card, and main prints two cards:
#include <stdio.h>
void print_border(void) {
printf("*********************\n");
}
void print_card(void) {
print_border();
printf("* Happy Birthday! *\n");
print_border();
}
int main(void) {
print_card();
printf("\n");
print_card();
return 0;
}
*********************
* Happy Birthday! *
*********************
*********************
* Happy Birthday! *
*********************
Want a wider border? Change one line in print_border, and all four borders change. Without the function, you'd have to edit four separate printf calls and hope you didn't miss one.
💡 Tip
Name functions after what they do, usually with a verb: print_menu, show_score, reset_board. If you can't describe a function in one short sentence, it's probably doing too much and should be split.
What can go inside a function body?
Anything you've written inside main so far: variables, if statements, loops, other function calls. Variables created inside a function belong to that function only and are created fresh on every call. (You'll learn the exact rules in scope and storage classes.)
#include <stdio.h>
void show_countdown(void) {
for (int seconds = 3; seconds > 0; seconds--) {
printf("%d... ", seconds);
}
printf("Go!\n");
}
int main(void) {
printf("Race 1: ");
show_countdown();
printf("Race 2: ");
show_countdown();
return 0;
}
Race 1: 3... 2... 1... Go!
Race 2: 3... 2... 1... Go!
Each call starts the loop from 3 again, because seconds is created anew every time show_countdown runs.
What is a void function?
A void function is a function with return type void: it does its job (usually printing something or changing something) but doesn't hand a value back to the caller. make_coffee, print_card and show_countdown are all void functions.
The void inside the parentheses is a separate thing: it says the function takes no parameters. So void make_coffee(void) reads as "returns nothing, takes nothing".
Why must a function be declared before it is used?
The C compiler reads your file from top to bottom, once. When it reaches a call, it must already know that the function exists and what it looks like. That's why, so far, every function was written above main.
If you move make_coffee below main, the compiler meets the call first and doesn't know the name yet. Recent versions of GCC (14 and later) stop with an error saying implicit declaration of function 'make_coffee'; older versions print a warning with the same text. Either way, it's a problem you must fix.
There are two fixes: define the function above its first call, or add a one-line prototype at the top that announces it:
void make_coffee(void); /* prototype: "this function exists" */
Prototypes let you keep main near the top of the file, where readers look first. They get their own lesson: C function prototypes.
Is main a function?
Yes. main is an ordinary function with one special rule: it's the function the operating system calls when your program starts. Its return type is int, and return 0; hands the value 0 back to the operating system, meaning "everything went fine". A non-zero value usually signals an error.
On Linux or macOS, you can see the value main returned right after running your program:
./coffee
echo $?
The second command prints 0 for the coffee program above.
📝 Note
You can't define a function inside another function in standard C. Every function definition sits at the "top level" of the file, side by side, like print_border, print_card and main above. If you know Python, this differs from Python functions, which can be nested.
Common mistakes with C functions
1. Forgetting the parentheses in a call
make_coffee; /* does nothing! */
make_coffee(); /* correct: calls the function */
Without (), the line just names the function and does not call it. GCC with -Wall warns that the statement is "a reference, not call, to function", but a warning is easy to miss.
2. Putting a semicolon after the header in a definition
void make_coffee(void); /* the semicolon ends it here... */
{
printf("Grinding beans...\n");
}
With the semicolon, the first line becomes a prototype, and the block below it belongs to nothing, so GCC reports a syntax error (it expects an identifier before the {). A definition header has no semicolon.
3. Calling a function before the compiler knows it
Calling a function that is defined further down, with no prototype above, triggers the "implicit declaration" error described earlier. Define it first or add a prototype.
4. Expecting a definition to run by itself
Writing a function doesn't run it. If nothing prints, check that main (directly or through another function) actually calls it.
Practice exercise
🏋️ Try it yourself
Write a void function print_menu that prints a small café menu: a title line --- Menu ---, then 1. Coffee, 2. Tea and 3. Juice. Call it from main twice, printing Table 4: before the first menu and Table 7: before the second.
Show solution
#include <stdio.h>
void print_menu(void) {
printf("--- Menu ---\n");
printf("1. Coffee\n");
printf("2. Tea\n");
printf("3. Juice\n");
}
int main(void) {
printf("Table 4:\n");
print_menu();
printf("Table 7:\n");
print_menu();
return 0;
}
Table 4:
--- Menu ---
1. Coffee
2. Tea
3. Juice
Table 7:
--- Menu ---
1. Coffee
2. Tea
3. Juice
What's next?
Your functions do the same thing every time. To make them flexible, like a coffee machine that asks "small or large?", you need inputs. Continue with C function parameters and arguments.
Key takeaways
- A function is a named block of code that does one job; you define it once and call it as often as you need.
- A definition has a return type, a name, a parameter list in parentheses and a body in curly braces.
- Call a function by writing its name with parentheses and a semicolon, like make_coffee();
- On a call, the caller pauses, the function body runs top to bottom, then control returns to the line after the call.
- void as the return type means the function gives nothing back; (void) as the parameter list means it takes no inputs.
- The compiler must know a function before its first call: define it above, or add a prototype.
- main is a function too, called by the operating system; it returns 0 to report success.
Frequently asked questions
What is a function in C?
A function is a named block of code that performs one task. You define it once with a return type, name, parameter list and body, and run it by calling its name followed by parentheses.
What is the difference between defining and calling a function?
Defining a function writes its code and gives it a name; nothing runs yet. Calling a function, for example greet();, actually runs its body. You define once and can call many times.
What does void mean in a C function?
As a return type, void means the function returns no value. Inside the parentheses, as in int main(void), it means the function takes no parameters.
Why do I get an implicit declaration error in C?
You called a function before the compiler saw its definition or prototype. Move the definition above the call or add a prototype near the top of the file.
Is main a function in C?
Yes. main is the function the operating system calls when your program starts. It returns an int, and returning 0 means the program finished successfully.
Can a function call another function in C?
Yes. Any function can call any other function the compiler already knows about. When the inner call finishes, control returns to the calling function.