float root ()
void printgreeting()
printgreeting();
Examples of "calling" functions which return a value will be
seen later.
#include<iostream.h>
#include<iomanip.h>
/* test program written by D C Smolarski, S.J.
for Math 10
Conversion of miles to kilometers */
void PrintHeader()
{
//*****************************
//* output a header
//*****************************
cout << " Miles Kilometers" << endl;
}
void Compute()
{
int num;
float miles, kilometers;
//*****************************
//* initialization
//*****************************
cout << setiosflags(ios::fixed);
cout << setprecision(1);
//*****************************
//* loop to print out 12 lines of output
//*****************************
for ( num=1 ; num <= 12 ; num++ )
{
miles = num*5.0;
kilometers = miles * 8.0/5.0;
cout << setw(6) << miles << setw(10) << kilometers << endl;
};
}
int main()
{
//*****************************
//* output a header
//*****************************
PrintHeader();
//*****************************
//* loop to print out 12 lines of output
//*****************************
Compute();
return 0;
}
It is permitted to declare constants and variables in the main body of the program file (e.g., right after the include statements) and not part of any function. Identifiers declared in this way are called global (in scope). If the same variable identifier is used both in a function and globally, the local identifier takes precedence. The global identifier is ignored.
A (vague) analogy can be made to the various laws in the United States. Federal laws bind equally across the country, but state laws are binding only in the given state. (Although in the case of laws, federal laws may nullify state laws.)
Any constant or variable declared globally is "visible" to all the code and thus can be called by any function or by the main program. Although having global constants for a program (for example, declaring one value of pi for the entire program) is sometimes considered good style, most authors consider it very poor programming style to use globally declared variables. Such variables can lead to errors and other problems, even though life may seem to be much easier when writing functions.
In this course, one should never use global variables! (Globally declared constants are permitted, however.)
This technique was not always used. A major problem in industry today is that older programs are generally unreadable, and so they cannot be maintained properly, and we do not always know why they are working (we hope) correctly.
Dr. Tom Manteuffel, who once worked at Los Alamos Lab in New Mexico, in a lecture complained about a typical program that he was asked to work on to speed up -- 5000 lines of code, no comments and one subroutine (written in an older style of FORTRAN "spaghetti" code)! Such code tends to be very difficult to read and contemporary programmers even wonder why it continues to work!
BE CAREFUL!!! There is no necessity for such statements to be in a procedure. Some procedures merely do calculations!
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1997, 1998, Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 5 December 1998.