C++ Functions
Think of functions as agents: they sense, think and act.

By convention, any functions that you declare and define should be named with a lower-case initial letter.

Functions are the jobs you need to coordinate in order to get a larger project done. As a programmer, think of yourself as the manager of an enterprise, the general of an army or the director of a movie. We'll pursue the cinematic metaphor. Imagine yourself as the director of a movie. You are in charge of everyone. You coordinate the work of the actors, cameramen, focus pullers, extras, carpenters, electricians, set decorators, prop handlers and sound recorders. Each has a job to do, and each may delegate smaller jobs to those beneath them. But you are in command of the big picture. It is your job to decide what is to be done, how to do it and who to assign to each task.

When you write an application, you are the director of a complex set of functions, your agents to whom you have assigned these specific tasks. Think of C++ functions as your agents. It is up to you to specify what each wil do and how. As as the creator and programmer of an artificial world, it is your responsibility to identify the jobs that need to be done, to describe each job or process as a C++ function, and to organize these functions and their relationships to one another in order to accomplish the task at hand. Think of your application as an organized group of functions; think of it as the social organization of your crew of employees:

For further flexibility, any function, may call any other function, just as any crew member may call upon another for assistance. You must organize all of this. You must describe the "chain of command:"

The Director calls, "Lights 3." (A function call with one parameter and one return value.)

The Director calls, "Camera 2, 5." (A function call with two parameters and one return value.)

The Director calls, "Action." (A function call with no parameters and no return value.)

These calls to action are called function calls in programming.


An Abstract Overview

A function may accept parameters, do something in its body, and directly return only one value.
However, it may also change the values of any number of global variables and the properties of any number of components.

// An abstract description of a function:
returnType functionName (parameters) {
   // do this (the function body)
   return returnValue;
}


It might be helpful to think of a function as an agent with a name, who can sense, think, and act:

// Thinking of a function as an agent:
reply functionName (instructions) {
   // do something (thought and action)
   return reply;
}

Or you may wish to think of a function as black box with a name, which has inputs and one output:

// Thinking of a function as a black box:
(output) functionName (inputs) {
   // do something (action and output)
   return returnValue;
}

You call a function by calling its name followed by the parameters, if there are any, in parentheses.

In summary:


An concrete example of a function with no returnValue and no parameters:

To indicate that the function expects no parameters and returns no returnValue, we insert the word void where the parameters would otherwise be.

// Ready to shoot!

void shooTheScene (void) {
   quietOnTheSet();
   lights();
   camera();
   action();
}

To call it we simply yell:

shootTheScene();

An concrete example of a function with one returnValue but no parameters:

To indicate that the function returns a returnValue but expects no parameters, we insert the data type
of the returnValue before the function name and the word void where the parameters would otherwise be.

// Is there enough film in the camera?
int howMuchFilmIsLeft (void) {
   int feetLeft = 1000 - feetUsed;
   return feetLeft;
}

Since the function has an integer return value, we can use the function call itself as a variable in another statement:
The function call will be replaced by its return value, the number of feet that are left.

if(howMuchFilmIsLeft() < 50) reloadTheCamera();

In other words, if there are less than 50 feet of film left, tell the cameraman to reload.


An concrete example of a function with no returnValue but several parameters:

To indicate that the function expects no returnValue but does take parameters, we insert the word void
where the returnValue would otherwise be and data types and variable names of the parameters .

// On to the next scene!
void prepareForNextScene (int act, int scene) {
   intScriptPage = act * 10 + scene;
   placesEveryone();
}

To call it we may simply say:

prepareForNextScene(3, 4);

An concrete example of a function with one returnValue and several parameters:

To indicate that the function expects one boolian (true or false) returnValue and two parameters, we insert the data type
of the returnValue bool and data types and variables int of the two parameters .

// Get the actors on stage!
bool actorsToYourMarks (int femaleLead, int maleLead) {
   while (femaleLead == notReady) {rehearse()};
   while (maleLead == notReady) {rehearse()};
   bool allReady = true;
   return allReady;
}

Since the function has a boolean returnValue, we can use the function call itself as a variable in a larger statement:
The function call will be replaced by its return value, and we can call action().

if(actorsToYourMarks(11, 27)) action();

In other words, as long as the male and female leads are not ready, they should rehearse.
When they are ready, we will call for action.