< Previous | Contents | Next >

Declaring Functions

Before you can call a function youve written, you have to declare it. One way to declare a function is to write a function prototypecode that describes the function. You write a prototype by listing the return value of the function (or void if the function returns no value), followed by the name of the function, followed by a list of parameters between a set of parentheses. Parameters receive the values sent as arguments in a function call.

Just before the main() function, I write a function prototype:

void instructions();

In the preceding code, I declared a function named instructions that doesnt return a value. (You can tell this because I used void as the return type.) The function also takes no values so it has no parameters. (You can tell this because theres nothing between the parentheses.)

Prototypes are not the only way to declare a function. Another way to accomplish the same thing is to let the function definition act as its own declaration. To do that, you simply have to put your function definition before the call to the function.


Hin t

image

Although you don’t have to use prototypes, they offer a lot of benefits—not the least of which is making your code clearer.

image

154 Chapter 5 n Functions: Mad Lib