<<== <= [table of contents] [search] [glossary] [feedback] => ==>>

[online C++ tutorial]Section 5: Functions

Section 5.3: Parameters to Functions

We've already seen some examples of parameters to functions. There are a lot of questions that remain. For example, let's take a look at the following code example:

//include this file for cout
#include <iostream.h>

// square a number and return it
double square(double value){
  value = value * value;
  return value;
}

int main() {
  // square 2.0 and print the result
  double a = 2.0;
  double a_squared = square(a);
  cout << a << " squared is: " << a_squared << endl;
 
  // try it again without resetting the value of a
  a_squared = square(a);
  cout << a << " squared is: " << a_squared << endl;
 
  return 0;
}

Look carefully. Should the program print:

2 squared is: 4
2 squared is: 4

OR

2 squared is: 4
4 squared is: 16

Pass by Value or Reference?

There is a subtle difference here: do we want changes made within a function on parameters passed to a function to affect other parts of the program? This is known as passing parameters by value or by reference. If you tried the code above you would notice that it prints out:

2 squared is: 4
2 squared is: 4

That is because our square function accepts it's parameter by value and not by reference. What this means is that the function is meant to make a copy of the value of the parameter and use this copy for all computations done in the function. Thus when the function exits all changes made by the function to the parameter are gone. When square was originally called, the value of a was 2. When it was called again the value remained 2. So how can you specify that you want to pass a parameter by reference? It's easy. Just put a & before the name of the parameter being passed in. For instance:

// square a number and return it
double square(double& value){
  value = value * value;
  return value;
}

Try the above example again with this small modification and see what happens. Notice a problem? Now the output of the program is:

4 squared is: 4
16 squared is: 16

The value that we are passing to the square function must be saved before we pass it, so we can print out the correct value. So the new source would look like:

//include this file for cout
#include <iostream.h>

// square a number and return it
double square(double& value){
  value = value * value;
  return value;
}

int main() {
  // square 2.0 and print the result
  double a = 2.0;
  double old_a = a;
  double a_squared = square(a);
  cout << old_a << " squared is: " << a_squared << endl;
 
  // try it again without resetting the value of a
  old_a = a;
  a_squared = square(a);
  cout << old_a << " squared is: " << a_squared << endl;
 
  return 0;
}

So what can I pass as a parameter?

A parameter to a function can be a variable, an instance of an object, a pointer (which we'll discuss in section 8), and even a pointer to a function! Just about anything can be a parameter to a function.

Function Name Spaces

A function only knows about two kinds of data: data that is global to the program and data that is passed in to the function in the form of parameters. This restriction keeps functions from getting too complex to manage. You should make a habit of not using any global data in functions, if possible. This is a good programming practice. The idea is to keep functions simple (and thus easy to debug).

Here is one common mistake when starting out with a programming language like c++:

int MultiplyTwoIntegers (int a, int b){
   int a;
   int d = a * b;
   return d;
}

Do you see the error? The line "int a;" is a mistake. What will actually happen? Well, most compilers will let you do this. What will happen is the second declaration of a will take precedence over the first. Therefore the value of a will be undefined and the value of d will also be undefined and unpredictable. Any variable that is passed in as a parameter should never be re-declared inside the function.


<<== <= [table of contents] [search] [glossary] [feedback] => ==>>