Section 2.2
Functions (I).
cplusplus.com

Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming in C++ can offer us.

A function is a block of instructions that is executed when it is called from some other point of the program. The following is its format:

type name ( argument1, argument2, ...) statement
where:
  � type is the type of data returned by the function.
  � name is the name by which it will be possible to call the function.
  � arguments (as many as wanted can be specified). Each argument consists of a type of data followed by its identifier, like in a variable declaration (for example, int x) and which acts within the function like any other variable. They allow passing parameters to the function when it is called. The different parameters are separated by commas.
  � statement is the function's body. It can be a single instruction or a block of instructions. In the latter case it must be delimited by curly brackets {}.

Here you have the first function example:

// function example
#include <iostream.h>

int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
  return 0;
}
The result is 8

In order to examine this code, first of all remember something said at the beginning of this tutorial: a C++ program always begins its execution with the main function. So we will begin there.

We can see how the main function begins by declaring the variable z of type int. Right after that we see a call to addition function. If we pay attention we will be able to see the similarity between the structure of the call to the function and the declaration of the function itself in the code lines above:


The parameters have a clear correspondence. Within the main function we called to addition passing two values: 5 and 3 that correspond to the int a and int b parameters declared for the function addition.

At the moment at which the function is called from main, control is lost by main and passed to function addition. The value of both parameters passed in the call (5 and 3) are copied to the local variables int a and int b within the function.

Function addition declares a new variable (int r;), and by means of the expression r=a+b;, it assigns to r the result of a plus b. Because the passed parameters for a and b are 5 and 3 respectively, the result is 8.

The following line of code:

return (r);
finalizes function addition, and returns the control back to the function that called it (main) following the program from the same point at which it was interrupted by the call to addition. But additionally, return was called with the content of variable r (return (r);), which at that moment was 8, so this value is said to be returned by the function.
The value returned by a function is the value given to the function when it is evaluated. Therefore, z will store the value returned by addition (5, 3), that is 8. To explain it another way, you can imagine that the call to a function (addition (5,3)) is literally replaced by the value it returns (8).

The following line of code in main is:

cout << "The result is " << z;
that, as you may already suppose, produces the printing of the result on the screen.

Scope of variables [re]

You must consider that the scope of variables declared within a function or any other block of instructions is only their own function or their own block of instructions and cannot be used outside of them. For example, in the previous example it had been impossible to use the variables a, b or r directly in function main since they were local variables to function addition. Also, it had been impossible to use the variable z directly within function addition, since this was a local variable to the function main.

Therefore, the scope of local variables is limited to the same nesting level in which they are declared. Nevertheless you can also declare global variables that are visible from any point of the code, inside and outside any function. In order to declare global variables you must do it outside any function or block of instructions, that means, directly in the body of the program.

And here is another example about functions:

// function example
#include <iostream.h>

int subtraction (int a, int b)
{
  int r;
  r=a-b;
  return (r);
}

int main ()
{
  int x=5, y=3, z;
  z = subtraction (7,2);
  cout << "The first result is " << z << '\n';
  cout << "The second result is " << subtraction (7,2) << '\n';
  cout << "The third result is " << subtraction (x,y) << '\n';
  z= 4 + subtraction (x,y);
  cout << "The fourth result is " << z << '\n';
  return 0;
}
The first result is 5
The second result is 5
The third result is 2
The fourth result is 6

In this case we have created the function subtraction. The only thing that this function does is to subtract both passed parameters and to return the result.

Nevertheless, if we examine the function main we will see that we have made several calls to function subtraction. We have used some different calling methods so that you see other ways or moments when a function can be called.

In order to understand well these examples you must consider once again that a call to a function could be perfectly replaced by its return value. For example the first case (that you should already know beacause it is the same pattern that we have used in previous examples):

z = subtraction (7,2);
cout << "The first result is " << z;

If we replace the function call by its result (that is 5), we would have:
z = 5;
cout << "The first result is " << z;

As well as

cout << "The second result is " << subtraction (7,2);
has the same result as the previous call, but in this case we made the call to subtraction directly as a parameter for cout. Simply imagine that we had written:
cout << "The second result is " << 5;
since 5 is the result of subtraction (7,2).

In the case of

cout << "The third result is " << subtraction (x,y);
The only new thing that we introduced is that the parameters of subtraction are variables instead of constants. That is perfectly valid. In this case the values passed to the function subtraction are the values of x and y, that are 5 and 3 respectively, giving 2 as result.

The fourth case is more of the same. Simply note that instead of:

z = 4 + subtraction (x,y);
we could have put:
z = subtraction (x,y) + 4;
with exactly the same result. Notice that the semicolon sign (;) goes at the end of the whole expression. It does not necessarily have to go right after the function call. The explanation might be once again that you imagine that a function can be replaced by its result:
z = 4 + 2;
z = 2 + 4;

Functions with no types. The use of void.

If you remember the syntax of a function declaration:
type name ( argument1, argument2 ...) statement
you will see that it is obligatory that this declaration begins with a type, that is the type of the data that will be returned by the function with the return instruction. But what if we want to return no value?

Imagine that we want to make a function just to show a message on the screen. We do not need it to return any value, moreover, we do not need it to receive any parameters. For these cases, the void type was devised in the C language. Take a look at:

// void function example
#include <iostream.h>

void dummyfunction (void)
{
  cout << "I'm a function!";
}

int main ()
{
  dummyfunction ();
  return 0;
}
I'm a function!

Although in C++ it is not necessary to specify void, its use is considered suitable to signify that it is a function without parameters or arguments and not something else.

What you must always be aware of is that the format for calling a function includes specifing its name and enclosing the arguments between parenthesis. The non-existence of arguments does not exempt us from the obligation to use parenthesis. For that reason the call to dummyfunction is

dummyfunction ();
This clearly indicates that it is a call to a function and not the name of a variable or anything else.

© The C++ Resources Network, 2000-2003 - All rights reserved

Previous:
2-1. Control structures.

index
Next:
2-3. Functions (II).