Function Arguments

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function. They behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit. As with local variables, you can make assignments to a function’s formal parameters or use them in any allowable C/C++ expression. Even though these variables perform the special task of receiving the value of the arguments passed to the function, they can be used like any other local variable.

In general, subroutines can be passed arguments in one of two ways. The first is called call by value. This method copies the value of an argument into the formal parameter of the subroutine. Changes made to the parameters of the subroutine have no effect on the variables used to call it. Call by reference is the second way a subroutine can have arguments passed to it. In this method, the address of an argument is copied into the parameter. Inside the subroutine, the argument is accessed through this address. This means that changes made to the parameter affect the variable used to call the routine.

By default, C and C++ use call by value to pass arguments. This means that you generally cannot alter the variables used to call the function. Consider the following function:

int sqr(int x) {   x = x*x;   return x; }

In this example, when the assignment x = x * x takes place, the only thing modified is the local variable x. The argument used to call sqr( ) still has its original value.

Remember that only a copy of the value of the argument is passed to a function. What occurs inside the function has no effect on the variable used in the call.

Passing Pointers

Even though C and C++ use call-by-value parameter passing by default, it is possible to manually construct a call by reference by passing a pointer to the argument. Since this passes the address of the argument to the function, it is then possible to change the value of the argument outside the function.

Pointers are passed to functions just like any other value. Of course, it is necessary to declare the parameters as pointer types. For example, the function swap( ), which exchanges the value of its two integer arguments, is shown here:

// Use pointer parameters. void swap(int *x, int *y) {   int temp;   temp = *x;  // save the value at address x    *x = *y;    // put y into x    *y = temp;  // put x into y  } 

It is important to remember that swap( ) (or any other function that uses pointer parameters) must be called with the addresses of the arguments. The following fragment shows the correct way to call swap( ):

int a, b; a = 10; b = 20; swap(&a, &b);

In this example, swap( ) is called with the addresses of a and b. The unary operator & is used to produce the addresses of the variables. Therefore, the addresses of a and b, not their values, are passed to the function swap( ). After the call, a will have the value 20 and b will have the value 10.

Reference Parameters

In C++, it is possible to automatically pass the address of a variable to a function. This is accomplished using a reference parameter. When using a reference parameter, the address of an argument is passed to the function and the function operates on the argument, not a copy.

To create a reference parameter, precede its name with the & (ampersand). Inside the function, you can use the parameter normally, without any need to use the the * (asterisk) operator. The compiler will automatically dereference the address for you. For example, the following creates a version of swap( ) that uses two reference parameters to exchange the values of its two arguments:

// Use reference parameters. void swap(int &x, int &y) {   int temp;   temp = x;  // save the value at address x    x = y;     // put y into x    y = temp;  // put x into y  }

When invoking swap( ), you simply use the normal function-call syntax. For example,

int a, b; a = 10; b = 20; swap(a, b); 

Because x and y are now reference parameters, the addresses of a and b are automatically generated and passed to the function. Inside the function, the parameter names are used without any need for the * operator because the compiler automatically refers to the calling arguments each time x and y are used.

Reference parameters apply only to C++.

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539 © 2008-2017.
If you may any questions please contact us: