| Lexington High School Mathematics | Math Dept. Front Page | Student Links | Family Links |
| C++ courses | Intro. Prog. I | Intro. Prog. II | AP Comp. Sci. | C++ Info Page |
Most larger C++ programs are written as a collection of smaller sections, called functions. Each function performs a specific task and can return a value such as a number or a string.
Every program has at least one function, the main function. Additional functions may be used in the main function or some other function, either as a command or as part of a math formula.
To create a function, two pieces of code must be added to the C++ program:
// Your name
// Date
// Assignment
#include <iostream.h> // Needed to use cout and cin
#include <math.h> // Needed to use math functions like ceil
#include "apstring.h" // Needed to use the apstring type.
float funcname (int x, float y); // Function prototypes go here. This is an example.
int main()
{
Here you can declare variables for the main program...
Commands go here...
return 0;
}
float funcname (int x, float y) // Function definitions go here. This is an example.
{
Here you can declare "local" variables to be used only inside this function...
Commands go here...
return valuetoreturn;
}