8. Writing and Using Functions

Chapter 8 - Writing and Using Functions

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Things Not to Do with Functions
When variables are used with different scope levels, it is possible to run into completely unexpected programming results, called side effects. For example, it is possible to use a variable of the same name with both file and local scopes. The scope rules state that the variable with a local scope (called a local variable) will take precedence over the variable with a file scope (called a global variable). That all seems easy enough, but let’s now consider some problem areas you might encounter in programming that are not so obvious.
Attempting to Access Out-of-Scope Identifiers
In the following example, four variables are given a local scope within the function main( ). Copies of the variables il and im are passed to the function iproduct( ). This does not violate scope rules. However, when the iproduct( ) function attempts to use the variable in, it cannot find the variable. Why? Because the scope of the variable was local to main( ) only.
/*
*   scopep.c
*   C program to illustrate problems with scope rules.
*   Function is supposed to form a product of three numbers.
*   Compiler signals problems since variable n isn’t known
*   to the function multiplier.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

int iproduct(int iw,int ix);

int main()
{
 int il=3;
 int im=7;
 int in=10;
 int io;

 io=iproduct(il,im);
 printf(“The product of the numbers is: %d\n”, io);

 return (0);
}

int iproduct(int iw,int ix)
{
 int iy;

 iy=iw
*ix*in;
 return(iy);
}
The C compiler issues a warning and an error message. It first reports a warning that the in variable is never used within the function and then the error message that in has never been declared in the function iproduct( ). One way around this problem is to give the variable, in, a file scope.
External vs. Internal Identifier Access
In the following application, the variable in is given a file scope. Making in global to the whole file allows both main( ) and iproduct( ) to use it. Also note that both main( ) and iproduct( ) can change the value of the variable. It is good programming practice not to allow functions to change global program variables if they are created to be truly portable.
/*
*   fscope.c
*   C program to illustrate problems with scope rules.
*   Function is supposed to form a product of three numbers.
*   Previous problem is solved, c variable is given file
*   scope.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

int iproduct(int iw,int ix);

int in=10;

int main()
{
 int il=3;
 int im=7;
 int io;
 io=iproduct(il,im);
 printf(“The product is: %d\n”, io);

 return (0);
}

int iproduct(int iw,int ix)
{
 int iy;

 iy=iw
*ix*in;
 return(iy);
}
This program will compile correctly and print the product 210 to the screen.
Internal vs. External Identifier Access
The scope rules state that a variable with both file and local scope will use the local variable value over the global value. Here is a small program that illustrates this point:
/*
*   lscope.c
*   C program to illustrate problems with scope rules.
*   Function forms a product of three numbers, but which
*   three?  Two are passed as function arguments. The
*   variable c has both a file and local scope.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

int iproduct(int iw,int ix);

int in=10;
int main()
{
 int il=3;
 int im=7;
 int io;

 io=iproduct(il,im);
 printf(“The product of the numbers is: %d\n”, io);

 return (0);
}

int iproduct(int iw,int ix)
{
 int iy;
 int in=2;

 iy=iw
*ix*in;
 return(iy);
}
In this example, the variable in has both file and local scope. When in is used within the function iproduct( ), the local scope takes precedence and the product of 3 * 7 * 2 = 42 is returned.
It’s Legal, But Don’t Ever Do It!
In the next C++ example, everything works fine up to the point of printing the information to the screen. The cout statement prints the values for il and im correctly. When selecting the in value, it chooses the global variable with file scope. The program reports that the product of 3 * 7 * 10 = 42 is clearly a mistake. You know that in this case the iproduct( ) function used the local value of in.
//
//  scopep.cpp
//  C++ program to illustrate problems with scope rules.
//  Function forms a product of three numbers. The n
//  variable is of local scope and used by function
//  product. However, main function reports that
//  the n value used is 10. What is wrong here?
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
int iproduct(int iw,int ix);

int in=10;

int main()
{
 int il=3;
 int im=7;
 int io;

 io=iproduct(il,im);
 cout << “The product of ” << il <<“
* ” << im
      << “
* ” << in << “ is: ” << io << endl;

 return (0);
}

int iproduct(int iw,int ix)
{
 int iy;
 int in=2;

 iy=iw
*ix*in;
 return(iy);
}
If you actually wanted to form the product with the global value of in, how could this conflict be resolved? C++ would permit you to use the scope resolution operator mentioned earlier in the chapter, as shown here:
iy=iw*ix*::in;
Overriding Internal Precedence
In this example, the scope resolution operator (::) is used to avoid conflicts between a variable with both file and local scope. The last program reported an incorrect product since the local value was used in the calculation. Notice in the following listing that the iproduct( ) function uses the scope resolution operator.
//
//  gscope.cpp
//  C++ program to illustrate problems with scope rules,
//  and how to use the scope resolution operator.
//  Function product uses resolution operator to “override”
//  local scope and utilize variable with file scope.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>

int iproduct(int iw,int ix);

int in=10;

int main()
{
 int il=3;
 int im=7;
 int io;
 io=iproduct(il,im);
 cout << “The product of ” << il <<“
* ” << im
      << “
* ” << in << “ is: ” << io;

 return (0);
}

int iproduct(int iw,int ix)
{
 int iy;
 int in=2;

 iy=iw
*ix*(::in);
 return(iy);
}
The scope resolution operators need not be enclosed in parentheses—they were used for emphasis in this example. Now, the value of the global variable, with file scope, will be used in the calculation. When the results are printed to the screen, you will see that 3 * 7 * 10 = 210.
The scope resolution operator is very important in C++. Additional examples illustrating the resolution operator are given starting with Chapter 16.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net