Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Chapter 21: Contending with Variables in Functions 267

Will the dual variable BOMBER.C program bomb?

Modify the source code for BOMBER.C in your editor. Change the main() func­ tion so that it reads:

int main()

{

char x;

printf(“Press Enter to drop the bomb:”);

x=getchar();

dropBomb();

printf(“Key code %d used to drop bomb.\n”,x); return(0);

}

What you’re doing is creating another x variable for use in the main() function. This variable operates independently of the x variable in the dropBomb() func­ tion. To prove it, save your changes to disk and then compile and run the program.

The output is roughly the same for this modified version of the program; the key code displayed for the Enter key “character” is either 10 or 13, depending on your computer.

See how the two x variables don’t confuse the computer? Each one works off by itself.

Variables in different functions can share the same name.

For example, you could have a dozen different functions in your program and have each one use the same variable names. No biggie. They’re all independent of each other because they’re nestled tightly in their own functions.

Variable x not only is used in two different functions — independently of each other — but also represents two different types of variable: a char­ acter and an integer. Weird.

Adding some important tension

Face it: You’re going nowhere fast as a game programmer with BOMBER.C. What you need is some tension to heighten the excitement as the bomb

268 Part IV: C Level

drops. Why not concoct your own delay() function to make it happen? Here’s your updated source code:

#include <stdio.h>

 

#define COUNT 20000000

/* 20,000,000 */

void dropBomb(void);

/* prototype */

void delay(void);

 

int main()

 

{

 

char x;

 

printf(“Press Enter to drop the bomb:”); x=getchar();

dropBomb();

printf(“Key code %d used to drop bomb.\n”,x); return(0);

}

void dropBomb()

{

int x;

for(x=20;x>1;x--)

{

puts(“

*”);

delay();

 

}

 

puts(“

BOOM!”);

}

 

void delay()

 

{

 

long int x;

 

for(x=0;x<COUNT;x++)

;

}

Mind the changes! Here they are:

#define COUNT 20000000

/* 20,000,000 */

A constant named COUNT is declared, equal to 20 million. It’s the delay value. (If the delay is too long, make the value smaller; if the delay is too short, make the value bigger.)

Chapter 21: Contending with Variables in Functions 269

Next, the delay() function must be prototyped:

void delay(void);

The delay() function is added into the dropBomb() function, right after puts() displays the “bomb:”

delay();

Finally, the delay() function is written. A long integer named x — a different x from any other used in any other function — is used in a for loop to create the delay.

Double-check your source code. Save to disk. Compile. Run. This time, the anticipation builds as the bomb slowly falls toward the ground and then —

BOOM!

The line used to create a constant (starting with #define) does not end with a semicolon! Go to Chapter 8 if you don’t know about this.

You can adjust the delay by changing the constant COUNT. Note how much easier it is than fishing through the program to find the exact delay loop and value — one of the chief advantages of using a constant variable.

Variables with the same names in different functions are different.

What’s the point again? I think it’s that you don’t have to keep on think­ ing of new variable names in each of your functions. If you’re fond of using the variable i in your for loops, for example, you can use i in all your functions. Just declare it and don’t worry about it.

How We Can All Share and

Love with Global Variables

Sometimes, you do have to share a variable between two or more functions. Most games, for example, store the score in a variable that’s accessible to a number of functions: the function that displays the score on the screen; the function that increases the score’s value; the function that decreases the value; and functions that store the score on disk, for example. All those functions have to access that one variable. That’s done by creating a global variable.

A global variable is one that any function in the program can use. The main() function can use it — any function. They can change, examine, modify, or do whatever to the variable. No problem.

270 Part IV: C Level

The opposite of a global variable is a local variable. It’s what you have seen used elsewhere in this book. A local variable exists inside only one function — like the variable x in the BOMBER.C program. The x is a local variable, unique to the functions in which it’s created and ignored by other functions in the program.

A global variable is available to all functions in your program.

A local variable is available only to the function in which it’s created.

Global variables can be used in any function without having to redeclare them. If you have a global integer variable score, for example, you don’t have to stick an int score; declaration in each function which uses that variable. The variable has already been declared and is ready for use in any function.

Because global variables exist all over the place, naming them is impor­ tant. After you declare x as a global variable, for example, no other func­ tion can declare x as anything else without ticking off the compiler.

Making a global variable

Global variables differ from local variables in two ways. First, because they’re global variables, any function in the program can use them. Second, they’re declared outside of any function. Out there. In the emptiness. Midst the chaos. Strange, but true.

For example:

#include <stdio.h>

int score;

int main()

{

Etc. . . .

Think of this source code as the beginning of some massive program, the details of which aren’t important right now. Notice how the variable score is declared. It’s done outside of any function, traditionally just before the main() function and after the pound-sign dealies (and any prototyping non­ sense). That’s how global variables are made.

If more global variables are required, they’re created in the same spot, right there before the main() function. Declaring them works as it normally does; the only difference is that it’s done outside of any function.