Access Modifiers

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

Functions in C vs. C++
C++ allows the use of several special features when writing functions. The ability to write inline functions is one such advantage. The code for an inline function is reproduced at the spot where the function is called in the main program. Since the compiler places the code at the point of the function call, execution time is saved when using short, frequently called functions.
C++ also allows function overloading. Overloading permits several function prototypes to be given the same function name. The numerous prototypes are then recognized by their type and argument list, not just by their name. Overloading is very useful when a function is required to work with different data types.
When Is a Function a Macro?
Think of the inline keyword as a directive or, better yet, a suggestion to the C++ compiler to insert the function inline. The compiler may ignore this suggestion for any of several reasons. For example, the function might be too long. Inline functions are used primarily to save time when short functions are called many times within a program.
//
//  inline.cpp
//  C++ program illustrates the use of an inline function.
//  Inline functions work best on short functions that are
//  used repeatedly. This example calculates the square
//  of an integer.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>

inline long squareit(int iValue) {return iValue * iValue;}
                               
int main()
{
 int iValue = 5;

 cout << squareit(iValue)
      << endl;

return (0);
}
The function squareit( ) is declared inline, which returns the square of the formal integer argument iValue. When the function main( ) calls function squareit( ), the compiler substitutes the function call with the expression iValue * iValue. In other words, the compiler replaces the function call with the function’s statement and also replaces the function’s parameters with the function’s arguments.
One advantage of inline functions versus macros is error checking. Invoking a macro with the wrong data type goes unchecked by the compiler. However, since an inline function has a prototype, the compiler performs type matching between the formal argument type(s) in the prototype and the actual argument(s) in the function call.
Prototyping Multiple Functions with the Same Name
The next example illustrates function overloading. Notice that two functions with the same name are prototyped within the same scope. The correct function will be selected based on the arguments provided. A function call to adder( ) will process integer or float data correctly.
//
//  ovrlod.cpp
//  C++ program illustrates function overloading.
//  Overloaded function receives an array of integers or
//  floats and returns either an integer or float product.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>

int adder(int iarray[]);
float adder(float farray[]);

int main()
{
 int iarray[7]={5,1,6,20,15,0,12};
 float farray[7]={3.3,5.2,0.05,1.49,3.12345,31.0,2.007};
 int isum;
 float fsum;
 isum=adder(iarray);
 fsum=adder(farray);
 cout << “The sum of the integer numbers is: ”
      << isum << endl;
 cout << “The sum of the float numbers is: ”
      << fsum << endl;

 return (0);
}

int adder(int iarray[])
{
 int i;
 int ipartial;

 ipartial=iarray[0];
 for (i=1;i<7;i++)
   ipartial+=iarray[i];
 return (ipartial);
}

float adder(float farray[])
{
 int i;
 float fpartial;

 fpartial=farray[0];
 for (i=1;i<7;i++)
   fpartial+=farray[i];
 return (fpartial);
}
There are a few programming snags to function overloading that must be avoided. For example, if a function differs only in the function type and not in the arguments, the function cannot be overloaded. Also, the following attempt at overloading is not permitted:
int yourfunction(int number)
int
yourfunction(int &value)   //not allowed
This syntax is not allowed because each prototype would accept the same type of arguments. Despite these limitations, overloading is a very important topic in C++ and is fully explored starting with Chapter 14.
Functions with Varying-length Formal Argument Lists
For functions with varying-length formal argument lists, you’ll use the ellipsis. The ellipsis is used when the number of arguments is not known. As such, they can be specified within the function’s formal argument statement. For example:
void yourfunction(int t,float u,...);
This syntax tells the C compiler that other arguments may or may not follow t and u, which are required. Naturally, type checking is suspended with the ellipsis.
The following C program demonstrates how to use the ellipsis. You may want to delay an in-depth study of the algorithm, however, until you have a thorough understanding of C string pointer types (see Chapters 9 and 10).
/*
*   elip.c
*   A C program demonstrating the use of ... and its support
*   macros va_arg, va_start, and va_end
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

void vsmallest(char
*szmessage, ...);

int main()
{
 vsmallest(“Print %d integers, %d %d %d”,10,4,1);

 return(0);
}

void vsmallest(char
*szmessage, ...)
{
 int inumber_of_percent_ds=0;
 va_list type_for_ellipsis;
 int ipercent_d_format = ‘d’;
 char
*pchar;
 pchar=strchr(szmessage,ipercent_d_format);
 while(*++pchar != ‘\0’) {
   pchar++;
   pchar=strchr(pchar,ipercent_d_format);
   inumber_of_percent_ds++;
 }
 printf(“print %d integers,”,inumber_of_percent_ds);

 va_start(type_for_ellipsis,szmessage);

 while(inumber_of_percent_ds—)
   printf(“ %d”,va_arg(type_for_ellipsis,int));

 va_end(type_for_ellipsis);
}
The function vsmallest( ) has been prototyped to expect two arguments, a string pointer, and an argument of type ..., or a varying length argument list. Naturally, functions using a varying-length argument list are not omniscient. Something within the argument list must give the function enough information to process the varying part. In ELIP.C, this information comes from the string argument.
In a very crude approach, vsmallest( ) attempts to mimic the printf( ) function. The subroutine scans the szmessage format string to see how many %ds it finds. It then uses this information to make a calculated fetching and printing of the information in the variable argument. While this sounds straightforward, the algorithm requires a sophisticated sequence of events.
The strchr( ) function returns the address of the location containing the “d” in %d. The first %d can be ignored since this is required by the output message. The while loop continues processing the remainder of the szmessage string looking for the variable number of %ds and counting them (inumber_of_percent_ds). With this accomplished, the beginning of the output message is printed.
The va_start( ) macro sets the type_for_ellipsis pointer to the beginning of the variable argument list. The va_arg( ) support macro retrieves the next argument in the variable list. The macro uses its second parameter to know what data type to retrieve; for the example program, this is type int. The function vsmallest( ) terminates with a call to va_end( ). The last of the three standard C ellipsis support macros; va_end( ) resets the pointer to null.

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