Program Structure
Most c++ programs have a structure like the program in this
file
When we run the program, we get output that looks like:
The square of 27 is 729
The CUBE of 27 is 19683
- There is a main routine.
- There are a number of other functions
- What is a function in algebra? (f(x) = x^2+3x+2)
- Things between { and } go togeather. They mark a body
of code.
- In this case, we have the body for the main routine, the body for the
functions square and cube.
- When the computer runs the code, it runs the first instruction
in the main routine, then the next instruction, then the next
instruction, then ...
- Line 1: cout << "The square of 27 is ";
- Prints The square of 27 is to the screen
- Line 2: cout << square(27) ;
- calls the routine square with argument 27
- int square (int n) {
- Says that the routine will return an integer.
- Also says that the routine wants an integer as input.
- variable n has the value 27
- It calculates n*n (27*27) = 729
- It returns this value to the main routine.
- The main routine prints this value out.
- Line 3 cout << endl;
- Prints out an end-of-line marker.
Every C++ program
- Must contain a main routine.
- Should return a value
- In UNIX 0 usually means success
- In UNIX < 0 means failure.