A function definition specifies what the function does, and can occur in a few different places. Functions can be defined after the main function, as long as a function prototype has first been declared. The prototype contains the name, return type and arguments, but not the body of the function. Here, the term declaration is used for function prototypes that declare that the function exists. The term definition is used where the body of the function is actually specified. Normally, a .c file has a companion .h file that contains all the function prototypes. With the AVRlib, we normally use a number of .c files (e.g. timer.c or a2d.c) that contain function definitions. The .c files are compiled together with the .c file you write, and are specified in the makefile (see below). But, in order to use functions from those .c files, you first need to declare their prototypes. This is done by including the corresponding .h file (e.g. timer.h or a2d.h). Look in a .h file in the AVRlib to see what it looks like. Functions can also be defined before the main function, but this is generally considered bad style.
void checkButton(void) { ... }
The code inside the function is executed when it is "called" from the another function:
checkButton();
If the function returns a value, the keyword return must be present in the function, followed by the value to be returned. Once return is reached, the function exits and evaluates to the return value. If a function has arguments, they are normally passed as a value in parentheses after the function name in the function call. Inside the function, the arguments act like variables that have the value that has been passed. Take the following program for example:
u08 dumbfunction(u08 foo); // function prototype int main(void) { u08 bar; bar = dumbfunction(10); ... } u08 dumbfunction(u08 foo) { return (foo * 10); }Here the variable bar will end up with the value 100.