Skip to content

Functions#

C provides basic support for functions. Before a function can be used, it must be declared:

return-type name(param-type-1, param-type-2, ...);

The return-type is the data type of the function's return value. If the function does not return anything, we use void for return-type.

The param-types are the data types of the function's parameters. We can optionally provide names for these parameters as well:

return-type name(param-type-1 param1, param-type-2 param2, ...);

If the function does not take any parameters, then we just put void between the parentheses.

We also need to provide a definition for the function which contains the code that the function executes:

return-type name(param-type-1 param1, param-type-2 param2, ...) {
    // Code
}

A definition also counts as a declaration, so we do not need to explicitly declare a function if we have defined it.

Example

TODO