©1994-2003 Kevin Boone
Home     Section index     K-Zone home C++ tutorial (under construction)

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
cin
contents
comments
C++ tutorial: structure


C/C++ programs start at 'main'
Click here to have another look at the simple program 'welcome.cpp'. Note that the program has the line

void main (void)

This indicates the start of the program. It doesn't matter where 'main' appears in the program itself, the computer knows that this is where execution should start. Technically 'main' is a function (see next section). The word 'void' indicates that this particular function has no inputs and no outputs. The curly brackets '{' and '}' show that the two lines of code inside the brackets comprise the 'main' function.

C programs are divided into functions
In a C program, functions are the basic 'unit of organization' of the program. Of course it is always possible in principle to make the program all fit in the function 'main', but this will be very inefficient, and difficult to understand. All practical C programs are divided into functions; there may be only a few functions in a simple program, or a few million in a very large one.

A function is a self-contained block ofprogram statements with a name (e.g., 'main'). We will see later that a function can accept data from other functions, and provide data to other fumctions. The statements in a functions are enclosed in curly braces: '{' at the start and '}' at the end.

In C++, rather than C, there is a higher level organizing principle: the class. A class is a group of functions and the data on which they operate. C++ programs usually have between one and a few hundred classes. The use of classes does not mean that functions are unimportant; the class consists of functions, after all. But it does lead to a slightly different way of looking at problems.

For a very simple example of the use of functions to break a program into smaller parts, have a look at welcome2.cpp. This program has exactly the same output as the prvious one 'welcome.cpp', i.e.:

Welcome to BIS4221:
software engineering and management.

But it achieves it in a slightly different way. The function 'main' now looks like this:

     FirstLine ();
     SecondLine ();

FirstLine and SecondLine are themselves functions; one which prints the first line of the program, and one which prints the second. The program welcome2.cpp therefore has three functions: main, FirstLine and SecondLine. Note that the definitions of these three functions are all the same:

void main (void)...

void FirstLine (void)...

void SecondLine (void)...

In other words, all these functions have no inputs and no outputs (from the context of program design, writing to the screen does not consitute an output).