C Functions
A function is a named block of code that performs one task. You write it once and call it whenever you need that task done. Functions are how you break a big program into small, understandable pieces, avoid copy-pasting code, and give meaningful names to the steps of your solution.
You have been using functions all along: main is a function, and printf, strlen and sqrt are functions provided by the standard library. Now you will write your own.
Defining and calling a function
return_type name(parameters) {
// body
}- return_type: the type of value the function gives back, or
voidif it returns nothing. - name: how you call it; follows the same rules as variable names.
- parameters: inputs, in parentheses;
void(or empty) when there are none.
#include <stdio.h>
void say_hello(void) {
printf("Hello from a function!\n");
}
int main(void) {
say_hello(); // call it
say_hello(); // and again
return 0;
}Hello from a function!
Hello from a function!
A call is the function name followed by parentheses. When the call runs, execution jumps into the function body, runs it, and then continues right after the call.
Note: In C a function must be declared before it is called. Here
say_hellois defined abovemain, which satisfies that rule. The lesson C Function Declaration shows how to put functions belowmain.
Returning a value
A function with a non-void return type sends a result back with return. The call itself then becomes that value, so you can store it, print it, or use it in a bigger expression:
#include <stdio.h>
int square(int n) {
return n * n;
}
double celsius_to_fahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
int main(void) {
int s = square(7);
printf("7 squared is %d\n", s);
printf("3 squared plus 4 squared is %d\n", square(3) + square(4));
printf("25 C = %.1f F\n", celsius_to_fahrenheit(25));
return 0;
}7 squared is 49
3 squared plus 4 squared is 25
25 C = 77.0 F
return ends the function immediately. A function can have several return statements, for example one per branch of an if:
#include <stdio.h>
const char *size_label(int cm) {
if (cm < 150) {
return "small";
}
if (cm < 180) {
return "medium";
}
return "tall";
}
int max_of(int a, int b) {
return a > b ? a : b;
}
int main(void) {
int heights[] = {142, 175, 191};
for (int i = 0; i < 3; i++) {
printf("%d cm is %s\n", heights[i], size_label(heights[i]));
}
printf("max_of(8, 13) = %d\n", max_of(8, 13));
return 0;
}142 cm is small
175 cm is medium
191 cm is tall
max_of(8, 13) = 13
Functions calling functions
Functions can call other functions. Building small, focused functions and combining them is the heart of good program design:
#include <stdio.h>
#include <stdbool.h>
bool is_divisible(int n, int d) {
return n % d == 0;
}
bool is_leap_year(int year) {
return (is_divisible(year, 4) && !is_divisible(year, 100))
|| is_divisible(year, 400);
}
int days_in_year(int year) {
return is_leap_year(year) ? 366 : 365;
}
int main(void) {
int years[] = {1900, 2000, 2023, 2024};
for (int i = 0; i < 4; i++) {
printf("%d has %d days\n", years[i], days_in_year(years[i]));
}
return 0;
}1900 has 365 days
2000 has 366 days
2023 has 365 days
2024 has 366 days
Why use functions?
| Benefit | Example |
|---|---|
| Reuse | write is_leap_year once, call it everywhere |
| Readability | days_in_year(y) says what happens; the body says how |
| Testing | small functions are easy to check one by one |
| Easier changes | fix a bug in one place instead of every copy |
Common mistakes
int add(int a, int b) {
int result = a + b;
} // forgot return: the caller gets garbage
void greet(void) { ... }
int x = greet(); // error: void function has no value
say_hello; // no parentheses: does not call anythingTip: Turn on
-Wall. GCC warns when a non-void function can reach its end without returning a value — a bug that otherwise silently produces garbage.
Test yourself
What return type does a function that returns nothing have?
- int
- null
- void
- empty
What does return do inside a function?
- Prints a value
- Ends the function and sends a value back to the caller
- Restarts the function
- Declares a variable
Given int twice(int x) { return 2 * x; }, what is twice(twice(3))?
- 6
- 9
- 12
- 3