Notes N7

Math 10 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 10 Homepage | Return to Notes Listing Page]

Contents


C/C++ Functions (Procedures)

A function is written in a way very similar to the way a main program is written. The major differences are as follows:

Calling Process

A procedure is "called" by simply listing its identifier as an independent C/C++ statement. For example, "void" function, printgreeting would be called as follows:
        printgreeting();
Examples of "calling" functions which return a value will be seen later.

Location of Function Code

The code for a function is usually placed after the #include statements (and other type definitions) and before the main program (or other function) for which it is defined. Later we will learn how to use "prototypes" which can avoid any concern about the exact order of the code of functions.

Sample Program with Functions

   #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;
    }

Variable "Scoping"

Functions may have variables declared in them. If so, these variables are said to be local to the function and cannot be used outside of it. The "scope" of the variable is local. This also includes variables declared in main() which is no more than a function that is always called first.

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.)

Programming Hints

Modern programming techniques emphasize subprograms. The main program can be very short -- it may only consist of calls to subprograms. This can aid readability and debugging of errors.

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!

Caution

A common problem is a compulsion to always include read/write statements in procedures.

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.