Search Bar

Thursday, February 16, 2017

2.2 — Void

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. Since we haven’t covered what a pointer is yet, you don’t need to worry about this case for now. 🙂

No comments:

Post a Comment

whatiscpp.blogspot.com is a free website devoted to teaching you how to program in C++. Whether you’ve had any prior programming experience or not, the tutorials on this site will walk you through all the steps to write, compile, and debug your C++ programs, all with plenty of examples.