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.
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.
The function definitions can be moved to another cpp file, such as myFunctions.cpp.
The function prototypes can be moved to another h (header) file, such as myFunctions.h.
The program can then just use the include directive as follows.
#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).