Consider the following program :
//file : tri1.C
#include <iostream.h>
int main()
{
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
return(0);
}
It's just a program that prints a triangle on the screen. Now consider this program :
//file : tri2.C
#include <iostream.h>
int main()
{
cout << "Here's one triangle :" << endl;
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
cout << "Here are two triangles :" << endl;
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
cout << "Here are three triangles :" << endl;
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
return(0);
}
There is a ton of repeated code here. Granted, with the advent of
cut 'n paste technology, it's relatively easy to write this program.
But you can still get the feel of how cumbersome things could become.
Here's a much better way to write the preceeding program :
//file : tri3.C
#include <iostream.h>
void draw_tri();
int main()
{
cout << "Here's one triangle :" << endl;
draw_tri();
cout << "Here are two triangles :" << endl;
draw_tri();
draw_tri();
cout << "Here are three triangles :" << endl;
draw_tri();
draw_tri();
draw_tri();
return(0);
}
void draw_tri()
{
cout << " *" << endl;
cout << " * *" << endl;
cout << "*****" << endl;
cout << endl;
}
If you don't understand these examples, then ask!