Section 4 - The Anatomy of a Program

1A.4.1 The Hello World! Program, Dissected

scenic house

Remember that we are trying to write a program that makes the computer do something useful and/or entertaining (let's stay away from destructive for now).  

A C++ program is made up of one or more methods (also called functions). There is always a main() function, and there are usually others. For the first few lectures, we will only have the one main() function. For now, don't worry about what a function really is, just that it takes the following form:

<function header>
{
<function body>
}

You'll see that I constantly switch between the terms function and method.  Don't worry.  They mean the same thing, and since other programmers also use both terms you'll want to get comfortable with either one.

Look at our first program:

#include <iostream>
using namespace std;

int  main()
{
cout << "Hello World!\n";
return 0;
}

We see that there is one #include statement and one using namespace statement, followed by a method called main(). Let's forget about the #include and namespace statements for now and get on to the meat: the function, main().

We see that the method header is:

int main()

and the method body consists of the statements:

cout << "Hello World!\n";
return 0;

For now, think of every program as a set of program statements which are completely contained inside this int main() method:

main()
{
<program statement>;
<program statement>;
<program statement>;
<program statement>;
return 0;
}

In the Hello World! program's main() method, there is only one statement besides the final return 0:

cout << "Hello World!\n";

If the program had more statements, each one would be terminated by a semicolon:

int time;

cout <<  "Hello World!\n";
cout <<  "La Femme Nikita\n";
time = 24;
cout << "Jack Bauer has " << time << " hours left.";

Take a guess at what Hello World!'s one and  only statement does:

cout << "Hello World!\n";

If you guessed that it prints the words "Hello World!" on the computer screen, congratulations! I haven't put you to sleep yet.

In the second module, later this week, you will be given instructions on how to make this program run.  For now, you are just learning the theory.