Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

Chapter 6: Creating Functions

91

Variable Storage Types

Function variables are stored in three different places. Variables declared within a function are said to be local. In the following example, the variable localVariable is local to the function fn():

int globalVariable; void fn()

{

int localVariable;

static int staticVariable;

}

The variable localVariable doesn’t exist until execution passes through its declaration within the function fn(). localVariable ceases to exist when the function returns. Upon return, whatever value that is stored in

localVariable is lost. In addition, only fn() has access to localVariable — other functions cannot reach into the function to access it.

By comparison, the variable globalVariable is created when the program begins execution — and exists as long as the program is running. All func­ tions have access to globalVariable all the time.

The static variable staticVariable is a sort of mix between a local and a global variable. The variable staticVariable is created when execution first reaches the declaration — at roughly when the function fn() is called. The variable is not destroyed when program execution returns from the func­ tion. If fn() assigns a value to staticVariable once, it’ll still be there the next time fn() is called. The declaration is ignored every subsequent time execution passes through.

In case anyone asks, there is a fourth type, auto, but today it has the same meaning as local, so you rarely (if ever) see that declaration type anymore. So whoever asked you about it is probably just being a show off (or showing his age).

Including Include Files

It’s common to place function prototypes in a separate file (called an include file) that the programmer can then include in her C++ source files. Doing so sets the stage for a C++ preprocessor program (which runs before the actual compiler takes over) to insert the contents of a file such as filename, at the point of a statement such as #include “filename”.

92

Part II: Becoming a Functional C++ Programmer

A definition for a typical math include file looks like this:

//math include file:

//provide prototypes for functions that might be useful

//in more than one program

//abs - return the absolute value of the argument double abs(double d);

//square - return the square of the argument

double square(double d);

A program uses the math include file like this:

// MyProgram - #include “math”

using namespace std;

// my code continues here

The #include directive says, in effect, Replace this directive with the contents of the math file.

The #include directive doesn’t have the format of a C++ statement because it’s interpreted by a separate interpreter that executes before the C++ com­ piler starts doing its thing. In particular, the # must be in column one and an end-of-line terminates the include statement. The actual file name must be enclosed in either quotes or brackets. Brackets are used for C++ library func­ tions. Use the quotes for any includes that you create.

The C++ environment provides include files such as cstdio and iostream. In fact, it’s iostream that contains the prototype for the setf() function used in Chapter 4 to set output to hex mode.

For years, programmers followed the custom of using the extension .h to designate include files. In more recent years, the C++ ISO standard removed the .h extension from standard include files. (For example, the include file cstdio was previously known as stdio.h.) Many programmers still stub­ bornly cling to the “.h gold standard” for their own programs. (What’s in a name? Evidence that even high-tech folks have traditions.)