void f()
means that f
returns nothing. If void
returns nothing, then why we use it? What is the main purpose of void
?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- How do I get from a type to the TryParse method?
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
probably to tell the compiler " you dont need to push and pop all cpu-registers!"
Cause consider some situations where you may have to do some calculation on global variables and put results in global variable or you want to print something depending on arguments , etc.. In these situations you can use the method which dont return value.. i.e.. void
Because sometimes you dont need a return value. That's why we use it.
Here's an example function:
And now here's another function:
The second function is faster, it doesn't have to copy whole struct.
If you didn't have
void
, how would you tell the compiler that a function doesn't return a value?Sometimes it can be used to print something, rather than to return it. See http://en.wikipedia.org/wiki/Mutator_method#C_example for examples