- Learn C++ - http://www.learncpp.com -

2.2 — Void

Void is the easiest of the data types to explain. Basically, it means “no type”!

Consequentially, variables can not be defined with a type of void:

Void is typically used in several different contexts:

1) Most commonly, as a way to indicate that a function does not return a value:

2) In C, as a way to indicate that a function does not take any parameters:

The explicit use of keyword void to mean “no parameters” is a holdover from C, and is not required in C++. The following code is equivalent, and preferred in C++:

Rule: Use an empty parameter list instead of void to indicate no function parameters are expected

3) The void keyword has a third (more advanced) use in C++ that we cover in section 6.13 -- Void pointers [1]. Since we haven’t covered what a pointer is yet, you don’t need to worry about this case for now. 🙂

2.3 -- Variable sizes and the sizeof operator [2]
Index [3]
2.1 -- Fundamental variable definition, initialization, and assignment [4]