std::function

What Are the Four Types of Functions in C?

The four common types of functions in C differ by whether they take arguments and whether they return a value. Each type comes with a short example.

On this page
  1. The four types of functions
  2. One example of each
  3. Arguments and return values
  4. About () in C
  5. Other ways to group functions
  6. Storing these with std::function
  7. Declaring before use
  8. Common mistakes
  9. Summary

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:

  1. No arguments and no return value.
  2. No arguments, with a return value.
  3. Arguments, with no return value.
  4. 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, because say_hello returns nothing.
  • Missing return. In C++, reaching the end of a function that returns a value without a return statement is undefined behavior. The exception is main, which returns 0 if it reaches its end.
  • Wrong argument count or type. add(1) does not compile, because add takes two int arguments.

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.
  • void means no return value.
  • In C++, () means no parameters. In C, write (void) to say the same thing.
  • std::function can hold any of the four forms when the signature matches.

The complete guide to std::function

std::function in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

SOLID Principles in C++

Learn the five SOLID principles in C++ with short examples of single responsibility, open/closed, Liskov, interface segregation and dependency inversion.