View Single Post
Old May 6th, 2008, 2:49 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Some Beginner Questions

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:
C++ Syntax (Toggle Plain Text)
  1. int square(int value); // returns the square of 'value'
  2. void clearScreen(void); // clears the screen
If a function is not type void, you can use it in an expression:
C++ Syntax (Toggle Plain Text)
  1. int x = square(37);
If it is type void, you cannot:
C++ Syntax (Toggle Plain Text)
  1. 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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote