To share this page click on the buttons below;
Functions
Functions are (with variables) the main ingredient of C programs. A function is a reusable piece of code that performs a specific set of operations on variables and (optionally) produces an output value. A function is always identified by a unique name, each function can have:
- zero or more arguments which are the variables the function is able to operate on (those are the parameters of the function)
- a return value.
It is common though to have functions with no parametes and no return value: a function with no arguments has void as parameter and a function returning nothing has void as return type (see below what arguments and returning value are). Functions follow the same scheme of variables: they are declared, defined and used.
Declaration
Now suppose that we want to write a function that performs the sum of two integers.
We declare the function and we name it sum
in this way:
int sum(int b, int c);
Again with the function declaration we are making a promise to the compiler: somewhere there will be a function named sum
that takes 2 integers as parameters (a
and b
) and return an integer. This in fact is the meaning of the previous line: the arguments of the functions (a
and b
) with their types are listed after the name of the function (sum
), enclosed in round brackets and separated with commas; the return value type is specified by the first int
present at the beginning of the line.
So that a generic function declaration looks like:
<type of the returned value> <name>(<argument list>);
Definition
Now we can define the function. The first part of the definition is the same of the declaration (return type, name and argument list), but instead of the semicolon there is the body of the function enclosed in curly brackets. The body contains a series of statements that perform operation on the function arguments. When the execution hits the return
the function execution stops and the value written after the return is returned as the result of the function operations.
In this case the body consists only of the return statement: the sum of the arguments is returned.
int sum(int b, int c) {
return b + c;
}
Please note that a function returns in any case when the execution hits the closing curling brace at the end of the body if not return statement is processed before. Pay attention: if the function returns at the closing brace it will not be able to return any value and if the function is supposed to return a value the compiler will issue a warning, but the compilation will succeed. That might or not cause problems in your code depending how the function is used and can cause subtle bug. Better to read and fix all the compiler warning.
Use
Once we define the function we can the use it:
int sum(int a, int b);
int sum(int a, int b) {
return a + b;
}
int c;
c = sum(5,3);
/* c will now contain 8 i.e. the sum of 5 and 3 */
First we declare the sum
function and then we define it, then we declare c
and then we finally use our function. Please note that the previous portion of code it not a complete C program and will not work (as it is now).
The declaration of a function must always be placed before the use of the function, while its definition might be placed somewhere else.
Definition can work as declaration so you can declare and define a function at once but this is not a good programming practice (the reason will be explained later) so always first declare and then define your function.
A special function
Each C program has a special function that must be always present: the main one. The main function is the starting point of the program: from there it begins the execution of you program.
To share this page click on the buttons below;