The four types of functions
One common way to group functions in C is to ask two yes-or-no questions: does the function take arguments, and does it return a value? Each combination of answers is one type:
- No arguments and no return value.
- No arguments, with a return value.
- Arguments, with no return value.
- Arguments and a return value.
This is one common grouping, not a formal rule, but it is a useful way to think about a function’s shape.
One example of each
The examples below use C++ syntax, since this site covers C++. The four groupings apply to C as well.
#include <iostream>
// 1. No arguments, no return value
void say_hello() {
std::cout << "Hello\n";
}
// 2. No arguments, returns a value
int get_answer() {
return 42;
}
// 3. Takes arguments, no return value
void print_sum(int a, int b) {
std::cout << a + b << '\n';
}
// 4. Takes arguments and returns a value
int add(int a, int b) {
return a + b;
}
int main() {
say_hello();
int answer = get_answer();
print_sum(2, 3);
int total = add(4, 5);
std::cout << answer << ' ' << total << '\n';
}
This program prints Hello, then 5, then 42 9. Each function is defined above main, so no separate declarations are needed.
Arguments and return values
The names in a function’s definition are its parameters. The values passed in at the call are its arguments. The return type says what the function gives back. void means nothing comes back, so a void function needs no return value. A function with a non-void return type must return a value of that type.
About () in C
In C, an empty pair of parentheses in a function declaration has historically meant “unspecified parameters,” not “no parameters.” To say that a function takes none, write int get_answer(void). In C++, () means no parameters.
Other ways to group functions
Another common split is library functions and user-defined functions. Library functions come with the language or its standard library, such as std::sqrt from <cmath>. User-defined functions are the ones you write yourself, like the four examples above.
Storing these with std::function
Once you have functions like these, c std::function can hold them under one type, as long as the signature matches:
std::function<void()> greet = say_hello;
std::function<int()> answer = get_answer;
std::function<void(int, int)> show = print_sum;
std::function<int(int, int)> sum = add;
Passing one of these functions into another function is covered in c pass function as argument.
Declaring before use
A function must be declared before you call it. The examples avoid this by defining each function above main. If main came first, you would need a declaration such as int add(int a, int b); above it. A declaration like that is a prototype, and what is a prototype in c explains the idea.
Common mistakes
- Using the result of a void function.
int x = say_hello();does not compile, becausesay_helloreturns nothing. - Missing return. In C++, reaching the end of a function that returns a value without a
returnstatement is undefined behavior. The exception ismain, which returns 0 if it reaches its end. - Wrong argument count or type.
add(1)does not compile, becauseaddtakes twointarguments.
Summary
- The four types come from two questions: does the function take arguments, and does it return a value?
- Parameters are named in the definition, and arguments are passed at the call.
voidmeans no return value.- In C++,
()means no parameters. In C, write(void)to say the same thing. std::functioncan hold any of the four forms when the signature matches.