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

258 Part IV: C Level

void main()

{

printf("He calls me on the phone with nothing say\n");

printf("Not once, or twice, but three times a day!\n");

{jerk();printf("Bill is a jerk!\n");

printf("He insulted my wife, my cat, my mother\n");

printf("He irritates and grates, like no other!\n");

{jerk();printf("Bill is a jerk!\n");

printf("He chuckles it off, his big belly a-heavin'\n");

printf("But he won't be laughing when I get even!\n");

Figure 20-1:

 

{

jerk();

How a

 

 

 

 

 

 

printf("Bill is a jerk!\n");

function

}

 

 

 

 

works in a

 

 

 

program.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The computer still reads instructions in the source code from the top down in the main function. However, when the computer sees another function, such as jerk(), it temporarily sidesteps to run the instructions in that function. Then, it returns back to where it was.

Keep in mind that not all functions are as simplistic as jerk(). Most of them contain many lines of code — stuff that would be too complex and redundant to use all over the place in a program.

Prototyping Your Functions

Prototyping refers to the art of telling the compiler what’s demanded of a func­ tion. It may seem like a silly thing to do, but it is in fact a good way to ensure that functions are used properly — plus it helps you keep track of your code. I know, it sounds a little like the compiler doesn’t trust you. But you probably don’t trust it much either, so the respect is mutual.

Proto comes from the Greek word for first.

Typing comes from the Latin word for “what you do on a keyboard.”

Chapter 20: Writing That First Function 259

Prototypical prototyping problems

You have to do two things to appease the prototyping gods. First, you have to properly configure the jerk function itself. Change Line 19 in the BIGJERK2.C source code to read:

void jerk()

This line tells the compiler that the jerk() function returns no values. That takes care of any function should return a value type of errors. (I cover functions that return values in Chapter 22; functions that don’t return a value are void. So there.)

Second, you have to tell the compiler about the jerk() function way early in the program. Essentially, that’s what prototyping is all about: You tell the com­ piler, “Hello? There’s a jerk() function later in this program, and here’s what you should expect to see.” You do this by sticking a line up top that looks like the start of the jerk function — but ends with a semicolon:

void jerk(void);

Stick this line between the #include <stdio.h> and the int main() that starts the main() function. The first part of your program looks like this:

#include <stdio.h>

void jerk(void);

int main()

The void jerk(void); line is the prototype. It tells the compiler to expect a jerk() function later on in the source code. It also says that the function will be of the void type, and won’t return any values. Furthermore, the jerk() function doesn’t require any values, which is why a void is in its parentheses. It’s heavy-duty void material. Just follow along if you don’t understand it.

Make the editing changes per the preceding instructions. A final rendition of the BIGJERK2.C program is shown here:

#include <stdio.h>

void jerk(void);

int main()

{

printf(“He calls me on the phone with nothing say\n”);

260 Part IV: C Level

printf(“Not once, or twice, but three times a day!\n”); jerk();

printf(“He insulted my wife, my cat, my mother\n”); printf(“He irritates and grates, like no other!\n”); jerk();

printf(“He chuckles it off, his big belly a-heavin’\n”); printf(“But he won’t be laughing when I get even!\n”); jerk();

return(0);

}

/* This is the jerk() function */

void jerk()

{

printf(“Bill is a jerk\n”);

}

When you’re done, resave BIGJERK2.C to disk. Recompile, and you shan’t be bothered by the various warning errors again.

The prototype is basically a rehash of a function that appears later in the program.

The prototype must shout out what type of function the program is and

describe what kind of stuff should be between the parentheses.

The prototype must also end with a semicolon. This is muy importanto.

I usually copy the first line of the function to the top of the program, paste it in there, and then add a semicolon. For example, in BIGJERK2.C, I copied Line 21 (the start of the jerk function) to the top of the source code and pasted it in, adding the necessary voids and semicolon.

No, the main() function doesn’t have to be prototyped. The compiler is expecting it and knows all about it. (Well, almost. . . .)

Required prototyping is something they added to the C language after it

was first introduced. You may encounter older C source code files that seem to lack any prototyping. Back in the days when such programs were written (before about 1990), this was a common way of doing things.

A sneaky way to avoid prototyping problems

Only the coolest of the C language gurus do this trick — so don’t tell anyone that you found out about it in a For Dummies book! Shhhh!

Chapter 20: Writing That First Function 261

Face it: Prototyping is a mess. Why repeat a function’s definition at the top of the program when it’s so obviously presented when the function is written? Seems redundant, eh? Many others think so, which is why they code their pro­ grams upside down. To wit, here’s another rendition of the BIGJERK program:

#include <stdio.h>

/* the jerk() function */

void jerk(void)

{

printf(“Bill is a jerk\n”);

}

/* Program starts here */

int main()

{

printf(“He calls me on the phone with nothing say\n”); printf(“Not once, or twice, but three times a day!\n”); jerk();

printf(“He insulted my wife, my cat, my mother\n”); printf(“He irritates and grates, like no other!\n”); jerk();

printf(“He chuckles it off, his big belly a-heavin’\n”); printf(“But he won’t be laughing when I get even!\n”); jerk();

return(0);

}

Edit the source code for BIGJERK2.C and make the changes in the preceding program. Basically, you’re removing the prototype for jerk() and replacing it with the jerk() function itself. Note that the jerk() function is defined as void jerk(void), just like a prototype, but it’s the function itself.

Save the changed source code to disk as BIGJERK3.C. Compile and run. The output is the same, but by turning the function upside down, you have utterly removed the possibility of prototyping errors.

The program still starts at the main() function despite stacking any other functions before it. Yes, the compiler is that smart.

You don’t have to code your programs this way. Rarely do programmers know in advance which functions they need, so most programmers start out coding functions the way it was done earlier in this chapter. Only after the function is written do they cut and paste it to the top of the file.

Don’t add the semicolon to the function’s declaration when you list your functions first! If you do, you get one of those nasty parse errors.