< Previous | Contents | Next >

Defining Functions

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 parenthesesjust like a function prototype (except you dont 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 doesnt return any value, I dont 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

image

A function definition must match its prototype on return type and function name; otherwise, you’ll generate a compile error.

image