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

Chapter 22: Functions That Actually Funct 279

jerk(500);

This statement calls the jerk() function, which then repeats the mes­ sage 500 times.

The C-geek vernacular for sending a value of a variable to a function is “passed.” So you pass the value 3 to the jerk() function with this statement:

jerk(3);

The value you send along to a function is called a parameter. It can be said, in a nerdly way, that the jerk() function has one parameter, an integer variable (which is a number).

If you forget to put a value in the jerk() function, you see a Too few parameters type of error. That’s one of the reasons that you prototype functions. If you don’t, and you use jerk() to call the function, your program invariably screws up.

The variable repeat is not a global variable. Instead, it’s a value that’s passed to the jerk function, which that function then uses to do some­ thing wonderful.

Note that jerk() also retains its own variable, i. Nothing new there.

Avoiding variable confusion (must reading)

You don’t have to call a function by using the same variable name the func­ tion uses. Don’t bother reading that sentence twice. It’s a confusing concept, but work with me here.

Suppose that you’re in the main() function where a variable named count is used. You want to pass along its value to the jerk() function. You do so this way:

jerk(count);

This line tells the compiler to call the jerk() function, sending it along the value of the count variable. Because count is an integer variable, this strategy works just fine. But, keep in mind that it’s the variable’s value that is passed along. The name count? It’s just a name in some function. Who cares! Only the value is important.

In the jerk() function, the value is referred to by using the variable name repeat. That’s how the jerk() function was set up:

void jerk(int repeat)

280 Part IV: C Level

Whatever value is sent, however it was sent, is always referred to as repeat inside the function.

I bring this concept up because it’s confusing. You can call any function with any variable name. Only inside that function is the function’s own variable name used.

This topic is confusing because of the variable names used by the func­ tion. You can find this subject in your C manual as well. You see some function listed like this:

int putchar(int c);

This line indicates that the putchar() function requires an integer value, which it refers to as c. However, you can call this function by using any variable name or a constant value. It makes no difference. What’s impor­ tant, however, is that it must be an integer variable.

Sending More than One

Value to a Function

The tray you use to pass values along to functions is quite large. It can hold more than one item — several, in fact. All you have to do is declare the items inside the function’s parentheses. It’s like announcing them at some fancy diplomatic function; each item has a type and a name and is followed by a lovely comma dressed in red taffeta with an appropriate hat. No semicolon appears at the end because it’s a formal occasion.

For example:

void bloat(int calories, int weight, int fat)

This function is defined as requiring three integer values: calories, weight, and fat. But, they don’t all have to be the same type of variable:

void jerk(int repeat, char c)

Here you see a modification of the jerk() function, which now requires two values: an integer and a character. These values are referred to as repeat and c inside the jerk() function. In fact, the following source code uses said function:

#include <stdio.h>

void jerk(int repeat, char c);

int main()

Chapter 22: Functions That Actually Funct 281

{

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

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

printf(“He chuckles it off, his big belly a- heavin’\n”);

printf(“But he won’t be laughing when I get even!\n”); jerk(3,’!’);

return(0);

}

/* The jerk() function repeats the refrain for the value of the repeat variable*/

void jerk(int repeat, char c)

{

int i;

for(i=0;i<repeat;i++)

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

}

Type the preceding source code. You can start with the BIGJERK4.C source code as a base. You have to edit Line 3 to modify the jerk() function proto­ type; edit Lines 9, 12, and 15 to modify the way the jerk() function is called (remember to use single quotes); then, redefine the jerk() function itself; and change the printf() statement inside the function so that it displays the character variable c.

Save the file to disk as BIGJERK5.C.

Compile and run the program. The output looks almost the same, but you see the effects of passing the single-character variable to the jerk() function in the way the question marks and exclamation points appear:

He calls me on the phone with nothing say

Not once, or twice, but three times a day!

Bill is a jerk?

He insulted my wife, my cat, my mother

He irritates and grates, like no other!

Bill is a jerk?

Bill is a jerk?

He chuckles it off, his big belly a-heavin’

But he won’t be laughing when I get even!

Bill is a jerk!

Bill is a jerk!

Bill is a jerk!