A function is a named block of code that you run by calling its name. It can take inputs, called parameters, do some work, and send back a result, called a return value. Functions let you write a piece of logic once and reuse it wherever you need it.
The question asks about C, and the idea is the same in C and C++. The examples here use C++, since that’s the language of this site. The basic syntax works in C too, with small changes such as using printf in place of std::cout.
A small example
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(2, 3) << '\n';
}
Here add takes two int parameters and returns an int. The call add(2, 3) runs the body with a set to 2 and b set to 3, and the return statement sends back 5. The std::cout line prints that value.
The parts of a function
A function definition usually has four parts:
- Return type: the kind of value the function gives back, such as
int. A function that gives back nothing usesvoid. - Name: how you call it, such as
add. - Parameter list: the inputs in parentheses, each with a type.
- Body: the code between the braces. A function that returns a value uses a
returnstatement to send it back.
A void function doesn’t need a return statement at all.
Calling a function
To use a function, write its name followed by the arguments in parentheses, as in add(2, 3). Each argument’s value is copied into the matching parameter. Changing a parameter inside the function doesn’t change the variable that was passed in, unless the parameter is a reference, which is a C++ feature.
Declarations and definitions
A function declaration gives the compiler a function’s name, return type and parameter types, without a body. A definition includes the body. A declaration lets you call a function before its body appears in the file:
#include <iostream>
int add(int a, int b); // declaration
int main() {
std::cout << add(2, 3) << '\n';
}
int add(int a, int b) { // definition
return a + b;
}
In larger programs, declarations often go in header files, and definitions go in source files. That’s a common convention rather than a rule.
Functions versus macros
A function is not the same as a macro. A #define macro can look like a function call, such as SQUARE(x), but the preprocessor replaces its text before compilation. There is no call at runtime, and no types are checked. A real function has a type-checked parameter list, and each argument is evaluated once before the function body runs. For most jobs that compute a value, a function is the safer choice.
Why use functions
Functions keep code organized, make pieces easier to test on their own, and let you avoid repeating the same lines. If you find yourself copying a block of code, that block is often a good candidate for a function.
Common mistakes
- Leaving off the return type in a function definition. C++ doesn’t allow that for ordinary functions.
- Returning a value from a
voidfunction, which is a compile error. - Calling a function before it has been declared. C++ requires a declaration first, which is why the declaration example above matters.
- Passing the wrong number of arguments, or arguments the compiler can’t convert to the parameter types.
Quick summary
A function is a named, reusable block of code. It takes parameters, runs its body and may return a value. Declare it before you call it, and remember that a macro that looks like a function isn’t one.