External and Local Variables

The place where you declare a variable controls where it is visible. A variable declared outside any function is “external”: you can refer to it anywhere within the program. (External variables are called “global” in some other languages.)

A variable declared inside the braces of a function is “local.” You can refer to it inside the function but nowhere else. In VOLUME.C, the result variable is declared inside the sphere function:

float sphere( int rad )

{

float result;

.

.

.

}

Summary: Use external variables only when necessary.

Because it is local to the sphere function, the result variable cannot be used elsewhere in VOLUME.C. Making variables local whenever possible minimizes the risk that a variable's value will be changed accidentally in some other part of the program.

When a function receives arguments, the arguments become local variables within the function. The sphere function requires one argument, which it names rad. Within the function, rad is a local variable.