Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Modules and interfaces in C


1. Modules and interfaces in C

2. Functions
Here is a function doubleIt that takes one integer formal parameter and returns the doubled value of that parameter.

Thus, the function doubleIt can be used anywhere an integer is expected except that an actual parameter (with an actual value) needs to be provided in the call. Here is the C code.

Here is the output of the C code.


3. Call by value
When a function (or procedure) is called, a call-by-value mechanism is used.

The function gets a copy of the expression value and cannot directly change any actual parameter passed to the function. Here is the same program with some name changes. Formal parameter names for a function and local variable names declared within the function are totally separate from variables in other functions (such as the main function).

Here is the C code.

In the above program, the actual parameter x1 (that has an actual value) is matched with the formal parameter x3 (that is a placeholder for a value to be supplied).

Here is the output of the C code.


4. Prototype
Here is the code with the function moved below the main function and the prototype of the function doubleIt copied and adjusted to above the main function.

Here is the C code.

Here is the output of the C code.


5. Separation
The separation of prototypes (above the main function) and the function definitions (below the main function) will eventually allow the following.
#include "myFunctions.h"


6. Include once
A common feature in header files is to use compiler directives to only include header files once. This is important when they may each refer to the other as otherwise there might be an endless inclusion of files (recursively).

Here is an example, from Scene.h (a different project, but included here for reference).
#ifndef SCENE_H #define SCENE_H // ... content ... #endif // SCENE_H

Here is an example, from Player.h.
#ifndef PLAYER_H #define PLAYER_H // ... content ... #endif // PLAYER_H


7. End of page