A function bundles statements under a name. It has a return type, a name, a parameter list, and a body. main is just a function that the runtime calls first. Use void when a function returns nothing.
Remember
return_type name(parameter_list) {
/* body */
return value; /* omit for void */
}
void greet(const char *name);
int add(int a, int b);
Reference
Cheat Sheet
Keep these facts handy while you work with Function Basics in C.
Example 1 — A void function and a value-returning function
One function prints; the other computes.
C
#include <stdio.h>
void greet(const char *name) {
printf("Hello, %s!\n", name);
}
int add(int a, int b) {
return a + b;
}
int main(void) {
greet("Ada");
printf("2 + 3 = %d\n", add(2, 3));
return 0;
}
Output
Hello, Ada!
2 + 3 = 5
How It Works
greet returns void, so it is called as a statement.
add returns an int, so the call is used inside an expression.
Both functions are defined before main, so no prototypes are needed.
Edge Cases & Pitfalls
Check these before relying on Function Basics in real programs.
()
Empty parentheses
In C int f() means "unspecified parameters" in older standards. Write int f(void) for none.
ret
Missing return
A non-void function that ends without return gives an indeterminate value if the caller uses it.
Remember
Key Takeaways
Parts: type, name, parameters, body.
void: no return value.
Single job: keep functions small.
One line: A function bundles statements under a name.
Frequently Asked Questions
A function is a named, reusable block of code that can take parameters and return a value. Every C program has at least main.
Yes. A function can call any function that is declared before the call, including itself (recursion).
Continue with "C Declaration and Definition" next. Tell the compiler a function exists, then provide its body. The C Functions hub lists every topic in order.