Quote:
|
Originally Posted by ChevyCowboy15
like in that example why would you do that?? i thought that everything had to either be a char, string, int of some type, double and so on... why would you declare something void
|
The only things that have type
void are functions or pointers. A function of type
void simply means the function has no return value. A parameter list can also be
void, but this is not a case of having a type; rather, it's an indicator that there is no parameter.
Say you have two functions. One calculates the square of an integer, and the other clears the screen. Now, the first is going to need to return a value, whereas the second is not (thus, its return type is
void). The first function is also going to need a parameter (the number to calculate the square of), whereas the second is not going to need a parameter in this example; its parameter list will therefore be a simple
void which means it takes no parameters. The function prototypes might look like this:
int square(int value); // returns the square of 'value'
void clearScreen(void); // clears the screen
If a function is not type void, you can use it in an expression:
If it is type void, you cannot:
int y = clearScreen(); // error
The example above is an error because
clearScreen() does not return a value. If you've been exposed to other languages, you might be familiar with a distinction between functions (that return a value), and procedures or subroutines (which do not). In these languages, the
clearScreen() function above would instead be called a procedure or subroutine, to indicate it has no return value. In C and C++, however, they are all functions; whether they return a value or not affects how you use them, but not what they are.