Functions in C
A function in C is a named block of code that does one job and can be reused whenever you need it. You give it some inputs (parameters), it does its work, and it can hand back a result (a return value). Functions let you break a big program into small, testable pieces instead of one giant main.
Anatomy of a function
Every function has a return type, a name, a parameter list, and a body:
returnType functionName(parameterList) {
/* body: the work happens here */
return value; /* optional, depending on returnType */
}
Here is a complete example that adds two numbers:
#include <stdio.h>
/* Definition: int = return type, two int parameters */
int add(int a, int b) {
return a + b; /* hands the result back to the caller */
}
int main(void) {
int sum = add(4, 7); /* call the function with arguments 4 and 7 */
printf("Sum is %d\n", sum); /* prints 11 */
return 0;
}
The values 4 and 7 are arguments; inside add, they arrive as the parameters a and b.
Functions that return nothing
If a function just performs an action and returns no value, its return type is void:
#include <stdio.h>
void greet(const char *name) { /* void = returns nothing */
printf("Hello, %s!\n", name);
}
int main(void) {
greet("Jalgaon");
return 0;
}
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFunction prototypes
C reads your file top to bottom. If you call a function before it is defined, the compiler needs a prototype (a declaration) first so it knows the function's signature:
#include <stdio.h>
int square(int n); /* prototype: declared before use */
int main(void) {
printf("%d\n", square(5)); /* 25 */
return 0;
}
int square(int n) { /* full definition appears later */
return n * n;
}
Prototypes usually live in header files for larger programs. The ; at the end of a prototype (no body) is what distinguishes it from a definition.
Pass by value
In C, arguments are passed by value — the function gets a copy. Changing a parameter inside the function does not change the caller's variable:
void tryToChange(int x) {
x = 99; /* only the local copy changes */
}
/* After calling tryToChange(n), n is unchanged. */
To let a function modify the caller's data, pass a pointer to it.
Why use functions?
- Reuse: write once, call many times.
- Readability: a name like
calculateTaxdocuments intent. - Testing: small functions are easy to verify in isolation.
- Recursion: a function can even call itself — see Recursion in C.
Common mistakes
- Calling before declaring without a prototype, causing a compiler warning or error.
- Mismatched return type: declaring
intbut returning nothing, or vice versa. - Forgetting
returnin a non-void function — the caller gets an undefined value. - Expecting pass-by-value to modify the original variable; it won't. Use pointers for that.
- Too many responsibilities: a function should do one clear thing. Split it if it grows large.
FAQ
Can a function have no parameters? Yes. Write void in the list, e.g. int getValue(void), to say it takes no arguments.
Can functions return only one value? Directly, yes. To "return" more, use pointers as out-parameters or return a struct.
Continue at the hub C Programming, then explore Recursion in C and Pointers in C.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
