Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.
In both C and C++, the declaration func(void); means an empty argument list. The void keyword means “nothing” in this case (it can also mean “no type” in the case of pointers, as you’ll see later in this chapter).
The other option for argument lists occurs when you don’t know how many arguments or
what type of arguments you will have; this is called a variable argument list. This “uncertain argument list” is represented by ellipses (... ). Defining a function with a variable argument list is significantly more complicated than defining a regular function. You can use a variable
argument list for a function that has a fixed set of arguments if (for some reason) you want to Chapter 1: Data Abstraction
92
disable the error checks of function prototyping. Because of this, you should restrict your use of variable argument lists to C, and avoid them in C++ (where, as you’ll learn, there are much better alternatives). Handling variable argument lists is described in the library section of your local C guide.
Function return values
A C++ function prototype must specify the return value type of the function (in C, if you
leave off the return value type it defaults to int). The return type specification precedes the function name. To specify that no value is returned, use the void keyword. This will generate an error if you try to return a value from the function. Here are some complete function
prototypes:
int f1(void); // Returns an int, takes no arguments
int f2(); // Like f1() in C++ but not in Standard C!
float f3(float, int, char, double); // Returns a float
void f4(void); // Takes no arguments, returns nothing
To return a value from a function, you use the return statement. return exits the function, back to the point right after the function call. If return has an argument, that argument becomes the return value of the function. If a function says that it will return a particular type, then each return statement must return that type. You can have more than one return statement in a function definition:
//: C03:Return.cpp
// Use of "return"
#include <iostream>
using namespace std;
char cfunc(int i) {
if(i == 0)
return 'a';
if(i == 1)
return 'g';
if(i == 5)
return 'z';
return 'c';
}
int main() {
cout << "type an integer: ";
int val;
cin >> val;
cout << cfunc(val) << endl;
} ///:~
Chapter 1: Data Abstraction
93
In cfunc( ), the first if that evaluates to true exits the function via the return statement.
Notice that a function declaration is not necessary because the function definition appears
before it is used in main( ), so the compiler knows about it from that function definition.
Using the C function library
All the functions in your local C function library are available while you are programming in C++. You should look hard at the function library before defining your own function – there’s a good chance that someone has already solved your problem for you, and probably given it a
lot more thought and debugging.
A word of caution, though: many compilers include a lot of extra functions that make life
even easier and are very tempting to use, but are not part of the Standard C library. If you are certain you will never want to move the application to another platform (and who is certain of that?), go ahead –use those functions and make your life easier. If you want your application to be portable, you should restrict yourself to Standard library functions. If you must perform platform-specific activities, try to isolate that code in one spot so it can easily be changed when porting to another platform. In C++, platform-specific activities are often encapsulated in a class, which is the ideal solution.
The formula for using a library function is as follows: first, find the function in your
programming reference (many programming references will index the function by category as
well as alphabetically). The description of the function should include a section that