< Previous | Contents | Next >
Defining functions means writing all the code that makes the function tick. You define a function 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—just like a function prototype (except you don’t end the line with a semicolon). This is called the function
header. Then you create a block with curly braces that contains the instructions
to be executed when the function is executed. This is called the function body.
At the end of the Instructions program, I define my simple instructions() function, which displays some game instructions. Because the function doesn’t return any value, I don’t need to use a return statement like I do in main(). I simply end the function definition with a closing curly brace.
void instructions()
{
cout << "Welcome to the most fun you’ve ever had with text!\n\n"; cout << "Here’s how to play the game.. .\n";
}
Tra p
A function definition must match its prototype on return type and function name; otherwise, you’ll generate a compile error.