Wouldn't it be nice if there were an easy way to reuse code without copy-pasting all the time?
In order to use (or write) a function, you have to know (or decide) three things:
Example 2: the job of a Distance function is to calculate the distance between two points. To do the calculation, it needs to know the coordinates of the points. After it finishes, it brings back the distance (which is a real number).
A function doesn't need to bring back (return) anything. (For example, you could have a robot whose job is to assemble an object but leave it where it was - then, all it needs to do when it's done is tell you it's done.)
Example 3: the job of a DisplayMoney function is to print a number to the screen in a money format (i.e., 20 could be displayed as $20.00). So it needs to be given a number to display, but doesn't need to bring anything back.
So a function is a reusable piece of code that accepts some input, does some work with it, and (possibly) brings back some information.
returntype FunctionName (parameters);The prototype for the Distance function might look like this:
double Distance ( int x1, int y1, int x2, int y2 );double is the return type: the C++ type of what the function returns (brings back). As discussed in the previous section, a function doesn't have to return anything. If this is the case, the return type is void.
.Notice that it looks like I assigned the function to a variable(d). This is similar to writing int a = 5 + 7; - the expression on the right hand side is evaluated, and the resultant value is assigned to the variable on the left hand side. A function essentially evaluates to its return value. This allows me to put the result of a function into a variable. Realize that a function with a void return type does not return anything, so I can't assign its result to a variable.
.
.
//create point p1 with x,y-coordinates (5,0)
int p1_x = 5;
int p1_y = 0;//create point p2 with x,y-coordinates (5,5)
int p2_x = 5;
int p2_y = 5;//calculate distance between p1 and p2
double d = Distance(p1_x, p1_y, p2_x, p2_y);
.
.
.
A function call can go anywhere a normal expression can. For example, I can write
double d_and_then_some = Distance(p1_x, p1_y, p2_x, p2_y) + 3;
I can also print the result to the screen:
cout << "Distance is " << Distance(p1_x, p1_y, p2_x, p2_y) << endl;Since a function with a void return type doesn't return anything, I can't print its result to the screen. (It's possible that the function itself prints to the screen; for example, the DisplayMoney function.)
A function call may occur in any piece of code, including in a function.
As stated before, function definitions go after main. They take the form
returntype FunctionName(parameters)Notice that the first line of the function definition is exactly like the prototype, except without the ending semicolon. This is important: if your function definition doesn't match the prototype, you'll get a compilation error.
{
//code
}
Here's a sample definition for the SquareIt function:
//This function accepts a number and returns its square.Notice that I declared a new variable inside the function. That variable, square exists only inside the function: no other functions or programs can get to it. Of course it's a bit wasteful to declare a new variable here. A better implementation would be:
//(Obviously this isn't a function you'd actually write; it's just an example.)
double SquareIt(double x)
{
double square = x * x;
return square;
}
double SquareIt(double x)This function consists only of a return statement, which is perfectly fine. Notice that I can return an entire expression; C++ will evaluate it before sending its value back to where the function was called from.
{
return x * x;
}
More will be added to this tutorial as I find time. I hope it's been helpful to you, and if you have any problems, questions, or corrections please contact me.