CH - 7 Function

Understanding how to define and call functions, parameter passing, return types, and scope

If you remember the syntax of a function declaration:
type name ( argument 1, argument2.....) statement
you will see that the declaration begins with a type, that is the type of the function itself (i.e., the type of the datum that will be returned by the function with the return statement). 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. In this case we should use the void type specifier for the function. This is a special specifier that indicates
absence of type.

// void function example
#include <iostream>
using namespace std;

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

int main(){
  printmessage();
  return 0;
}

void can also be used in the function's parameter list to explicitly specify that we want the function to take no actual parameters when it is called. For example, function pr could have been declared as:

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

Although it is optional to specify void in the parameter list. In C++, a parameter list can simply be left blank if we want a function with no parameters.

What you must always remember is that the format for calling a function includes specifying its name and enclosing its parameters between parentheses. The non-existence of parameters does not exempt us from the obligation to write the parentheses. For that reason the call to printmessage is:
printmessage ( ) ;


The parentheses clearly indicate that this is a call to a function and not the name of a variable or some other C++
Statement. The following call would have been incorrect:
printmessage;

Arguments passed by value and reference


Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value.
This means that when calling a function with parameters, what we have passed to the function were copies of their values but never the variables themselves. For example, suppose that we called our first function addition using following code;

int x = 5, y = 3, z;
z = addition (x, y);

What we did in this case was to call to function addition passing the values of x and y, i.e. and respectively, but not the variables x and y themselves.

This way, when the function addition is called, the value of its local variables a and b become 5 and 3 respectively, but any modification to either a or b. Within the function addition will not have any effect in the values of x and y outside it, because variables x and y were not themselves passed to the function, but only copies of their values at the moment the function was called.

Fig. 7.1
Diag: Fig. 7.1


But there might be some cases where you need to manipulate from inside a function the value of an external variable, For that purpose we can use arguments passed by reference, as in the function duplicate of the following example:

//pasing parameters by reference
#include <iostream>
using namespace std;

void duplicate(int& a, int& b, int& c)
{
  a *= 2;
  b *= 2;
  c *= 2;
}

int main(){
  int x = 1, y = 3, z = 7;
  duplicate (x, y, z);
  cout << "x= " << x << ", y=" << y << ", z=" << z;
  return 0;
}
Fig. 7.2
Diag: Fig. 7.2

The first thing that should call your attention is that in the declaration of duplicate the type of each parameter was followed by an ampersand sign (&). This ampersand is what specifies that their corresponding argument
to be passed by reference instead of by value.


When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their
counterpart variables passed as arguments in the call to the function.