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

Chapter 22

Functions That Actually Funct

In This Chapter

Sending a value to a function

Sending multiple values to a function

Using the return keyword

Understanding the main() function

Writing tighter code

Afunction is like a machine. Although the do-nothing void functions that you probably have read about in earlier chapters are still valid functions,

the real value in a function is having it do something. I mean, functions must chew on something and spit it out. Real meat-grinder stuff. Functions that funct.

This chapter explains how functions can be used to manipulate or produce information. It’s done by sending a value to a function or by having a function return a value. This chapter explains how all that kooky stuff works.

Marching a Value Off to a Function

Generally speaking, you can write four types of functions:

Functions that work all by themselves, not requiring any extra input:

These functions are described in previous chapters. Each one is a ho-hum function, but often necessary and every bit a function as a function can be.

Functions that take input and use it somehow: These functions are passed values, as either constants or variables, which they chew on and then do something useful based on the value received.

Functions that take input and produce output: These functions receive something and give you something back in kind (known as generating a value). For example, a function that computed your weight based on your shoe size would swallow your shoe size and cough up your weight. So to speak. Input and output.

276 Part IV: C Level

Functions that produce only output: These functions generate a value or string, returning it to the program — for example, a function that may tell you where the Enterprise is in the Klingon Empire. You call the whereEnt() function, and it returns some galactic coordinates.

Any function can fall into any category. It all depends on what you want the function to do. After you know that, you build the function accordingly.

How to send a value to a function

Sending a value to a function is as easy as heaving Grandma through a plate glass window. Just follow these steps:

1.Know what kind of value you’re going to send to the function.

It can be a constant value, a number or string, or it can be a C language variable. Either way, you must declare that value as the proper type so that the function knows exactly what type of value it’s receiving: int, char, or float, for example.

2.Declare the value as a variable in the function’s parentheses.

Suppose that your function eats an integer value. If so, you need to declare that value as a variable that the function will use. It works like declaring any variable in the C language, though the declaration is made inside the function’s parentheses following the function’s name:

void jerk(int repeat)

Don’t follow the variable declaration with a semicolon! In the preceding example, the integer variable repeat is declared, which means that the jerk() function requires an integer value. Internally, the function refers to the value by using the repeat variable.

3.Somehow use the value in your function.

The compiler doesn’t like it when you declare a variable and then that variable isn’t used. (It’s a waste of memory.) The error message reads something like jerk is passed a value that is not used or Parameter ‘repeat’ is never used in function jerk. It’s a warning error — and, heck, it may not even show up — but it’s a good point to make: Use your variables!

4.Properly prototype the function.

You must do this or else you get a host of warning errors. My advice: Select the line that starts your function; mark it as a block. Then, copy it to up above the main() function. After pasting it in, add a semicolon:

void jerk(int repeat);

No sweat.

Chapter 22: Functions That Actually Funct 277

5.Remember to send the proper values when you’re calling the function.

Because the function is required to eat values, you must send them along. No more empty parentheses! You must fill them, and fill them with the proper type of value: integer, character, floater — whatever. Only by doing that can the function properly do its thing.

The parameter is referred to as an argument. This term gives you a tiny taste of C’s combative nature.

The name you give the function’s parameter (its passed-along variable, argument, or whatever) is used when you’re defining and prototyping the function, and inside the function.

You can treat the function’s parameter as a local variable. Yeah, it’s defined in the prototype. Yeah, it appears on the first line. But, inside the func­ tion, it’s just a local variable.

By the way, the variable name used inside the function must match the variable name defined inside the function’s parentheses. More on this later.

Information on passing strings to functions is provided in my book C All- in-One Desk Reference For Dummies (Wiley).

Sending a value to a function or getting a value back isn’t the same as using a global variable. Although you can use global variables with a function, the values the function produces or generates don’t have to be global variables. (Refer to Chapter 21 for more information about global variables.)

An example (and it’s about time!)

Blindly type the following program, a modification of the BIGJERK.C cycle of programs you work with in Chapter 20:

#include <stdio.h>

void jerk(int repeat);

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(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);

278 Part IV: C Level

return(0);

}

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

void jerk(int repeat)

{

int i;

for(i=0;i<repeat;i++) printf(“Bill is a jerk\n”);

}

You can edit this source code from the BIGJERK2.C file, but save this file to disk as BIGJERK4.C. It has some changes, mostly with the jerk() function and the statements that call that function. Don’t miss anything, or else you get some nasty error messages.

Compile and run.

The program’s output now looks something like this:

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

The jerk() function has done been modified! It can now display the litany’s refrain any old number of times. Amazing. And look what it can do for your poetry.

The details of how this program worked are hammered out in the rest of this chapter. The following check marks may clear up a few key issues.

Notice how the jerk() function has been redefined in the prototype:

void jerk(int repeat);

This line tells the compiler that the jerk() function is hungry for an integer value, which it calls repeat.

The new jerk() function repeats the phrase Bill is a jerk for what­ ever number you specify. For example: