This is a discussion on Non-member functions in C++ within the Programming forums, part of the Tutorials category; Non-member functions in C++:- You need to learn no new C++ in order to use non-member functions. They are specified ...
Non-member functions in C++:-
You need to learn no new C++ in order to use non-member functions. They are specified and used in a way that is very similar to methods. The prototype of one has exactly the same format as the definition of a method:
<return type> <function name> (<parameters>);
The only difference is that a non-member function is not associated with any class. It is therefore not defined in a class’s definition or implementation. The definition of a non-member function is split into two in the same way as a method. There is a prototype that specifies how the function works, and a separate implementation. It is possible to use a function based only on information from the prototype; it is not necessary to see the implementation.
Any program that uses a non-member function must include the function’s prototype. The prototype is added to the program file, normally after any #include lines but before the start of the main function:
#include <iostream>
using namespace std;
// prototypes of non-member functions go here
int main ()
{
// main function statements
return 0;
}
The prototypes have exactly the same format as the familiar definition of a method, except that the optional const keyword is not applicable since there is no instance of a class to remain constant when the function is called. For example, a non-member function might be used to convert a char variable storing a letter in lower case to its upper-case equivalent; its prototype would be:
char upperCase (char);
This is interpreted in exactly the same way as the prototype of a method. The function is called upperCase, it returns a char value, and it takes a single char value as a value parameter.
The implementation of the function can be written in one of two places. It can be included at the bottom of the single program file after the final closing brace:
#include <iostream>
using namespace std;
// prototypes of non-member functions go here
int main ()
{
// main function statements
return 0;
}
// function implementation goes here
Alternatively, and probably better (since the above approach can still lead to very long files), it can be written in its own file and linked with the main function when the whole program is compiled. Your Local guide should tell you how to go about writing programs that are stored in many files. Using separate files to store functions can make editing the program very much easier, since smaller files are generally easier to handle in text editors. This approach also makes it much easier to reuse nonmember functions in different programs.
The implementation of the function follows much the same format as the familiar implementation of a method except that there is no class name to include:
char upperCase (char c)
{
// implementation here
}
Non-member functions are called in the main function in a slightly different way to methods. But, while the syntax is different, the effect is very much the same. To call the upperCase function on a char variable ch the statement would be:
ch = upperCase (ch);
This statement would presumably convert the value of ch to its upper-case equivalent. The only real difference in the syntax is of course that there is once again no class instance to be named. Finally, non-member functions may also have reference parameters. These parameters have exactly the same effect, and are used in the same way, as reference parameters are with methods. And as before, a parameter is declared to be a reference parameter with the addition of an & symbol:
void swap (int&, int&);
This function is intended to swap the values of two integer variables, quite a common task. This is its prototype, declaring that it doesn’t return a value and that it requires two integer variables as reference parameters; there should really of course also be a comment to explain precisely what it does with these parameters.
This function prototype would be included at the top of the program file, above the main function. The implementation would then either be included at the bottom of the same program file after the final closing brace or would be stored in its own file. Wherever it is stored the implementation is:
// swap two integer values
void swap (int& a, int& b)
{
int temp = a;
a = b;
b = temp;
return;
}
This function can then be used in the main function. The prototype is included at the top and, in this example, the implementation of the function is included below the main function.
// swap.cc
//
// Swaps the values of two integer variables.
// Demonstrates declaration and use of
// non-member functions.
//
// Chapter 16.
//
// Author: AMJ
// Date: 19th December 2001
// Platform: g++, Linux
#include <iostream>
using namespace std;
void swap (int&, int&);
int main ()
{
int x, y;
cout << "Enter value for x: ";
cin >> x;
cout << "Enter value for y: ";
cin >> y;
cout << "Swapping . . ." << endl;
swap (x, y);
cout << "x is now " << x << " and y is now "
<< y << endl;
return 0;
}
// swap two integer values
void swap (int& a, int& b)
{
int temp = a;
a = b;
b = temp;
return;
}
The swap function also declares its own variable, temp. This variable can only be used inside this function; it is called a local variable. When the function has finished and control has been returned to the main function any local variables in the function are deleted and their values are lost forever. This is exactly the same as the use of local variables in methods.
Non-member functions can be used for a variety of purposes. They can be used to reduce the complexity of the main function and to share out the development work between many programmers. Functions should definitely be used if any task is to be used more than once in a particular program. Any code that is duplicated in a program should always be written as a function; it can be a huge waste of effort to have to test and correct the same code in a number of different places in a single program.
One of the advantages of using classes in programs is that they allow for code reuse. The same advantage applies to the use of functions; functions that have already been written and tested can be used again and again in other programs in the future.
Many new programmers are tempted to ignore the possibility of using functions at first. They sometimes prefer to continue to write their programs in single long files and to grapple with the unwieldy files that often result from this approach. If they are told that they have to use functions they will often be tempted to write the program in a single file first and to try and split it into functions later. This approach is an extremely complex task and is usually a sure recipe for disaster and a lot of extra work. Don’t be tempted to do it!
When a program is designed it should also be split down into functions; this should happen before any of the program is written. The functions should be developed separately in much the same way as a class’s methods. After the functions have been written and tested the development of the main function is usually little more than the relatively straightforward process of combining all the existing tested and working pieces of program. The use of functions makes the development of programs much easier. Each programmer has only to work on a small part of a program at any one time, and small programs are much easier to write than big ones.
Bookmarks