0 experience, getting into C++, really just not understanding why you'd want/need to use void

21    09 Jun 2016 23:04 by u/DANKGHIDORAH

So I'm just going through the material here, and I was digesting everything pretty seamlessly, until chapter 1.4. I really can't seem to grasp why void is important, or why it should or need to be used, and as I wasn't able to understand it, I just decided to keep reading hoping it would make sense to me. However, seeing it used continuously thereafter, I'm just not able to understand what's going on any further. Newly seeing it in conjunction with callers, which is also new for me, is just making me unable to get why it even needs to exist. I don't understand the point/purpose/use of it?

edit: Welp, thank you very much /v/programming. Somehow reading everything here has made me understand what void is in a way that "A return type of void means the function does not return a value," seemingly couldn't get through for me. You guys are great <3

27 comments

23

C++ is a strongly typed language, meaning in general you need to declare the type of things (int, float, string, void) before using them.

Declaring a function with a return type of void is just a way to tell the compiler that this is a function that is not supposed to return any value. That way the compiler can potentially introduce optimizations to your code, and also warn you of cases when you would accidentally do something like my_variable = function_that_returns_void();. In a case like this the compiler knows function_that_returns_void() is not supposed to return anything (if you declared it void when you wrote it), and can therefore tell you the code you just wrote is invalid.

In the original C-language you were allowed to declare functions without declaring the return type. The compiler would then assume automatically that this function is supposed to return an integer. Today in C++ functions MUST always declare the return type in advance (unless you explicitly allow undeclared types with special compiler settings). This ensures better type safety and consistency across your code.

So in order to always declare the return type of all your functions, you have to declare a function that returns nothing to be of type void.

Hope that helps. If not, ask and I'll try and clarify further.

-1

Non programmers really shouldn't be trying to answer this. Yours is the only correct response here so far.

1

I wonder if you or qwop, being real programmers, can reconcile all of the above with the use of "void" in the code snippet in the header image for this sub?

0

"This function takes no arguments."

3

C and C++ are strongly "typed" languages. Most of the time void is used just to indicate that a function doesn't return a value(a return value would have to have a type).

Void is essentially the typeless type. You can cast variables to and from void, also.

Oversimplifying, "void" kind of means "whatever".

edit: "cast"

3

Not "whatever", it means "nothing", "nothing, Lebowski!" "Vee are nihilists!"

4

What about void pointers, mein neger? That means "I know this is a pointer to an area of memory, but I have no clue as to what data type it is!"

1

The ability to reference a function using a void* variable is handy too, because then you can start doing things like this code snippet (Warning, my C++ is rusty so my syntax is probably off):

int incrementAndTransform(int i, void* (*transformer)(int i)) {
  return transformer(++i);
}
int add5Transformer(int i) {
  return i + 5;
}
int mod6Transformer(int i) {
  return i % 6;
}
int main() {
  int i = 0;
  int t = incrementAndTransform(i,add5Transformer);
  int result = incrementAndTransform(t,mod6Transformer);
  return 0;
}
1

Except you can cast things to and from void, but they don't disappear when they are void.

2

Void functions are pretty much for functions where you don't need to return a value.

1

Void is more of a C feature than a C++ feature. Chances are, if you find some code that uses void, it could be better written with C++ features such as templating, overloading, etc. It's mostly included in C++ for backwards compatibility with C.

Edit: As others have pointed out, this is apart from its usage as a return type from a function, where it means 'this function doesn't return anything'.

0

In my experience (next to none), you use void in cases like a menu function. You output to the screen a menu and it doesn't need to send anything back to the main function.

There's other similar cases but generally what I've seen is that it's just when you hav a function that doesn't need to send anything back to the main.

0

The simplest example I can think of, a logging function, you you really want it to return something?

-1

THE CONTENT BELOW IS REFERRING TO VOID AS A POINTER TYPE, NOT A RETURN TYPE

Really, it's not recommend (to my knowledge) for you to ever use it. There is lots of stuff like that in C++, there is a lot of power you are given that is easy to misuse and abuse. Your confusion now is why most start off learning python, although I think C++ is still great to learn with.

Void is a pointer to ANYTHING. You have no idea what it points to or why, and it isn't enforced as such to code. This gives it a huge amount of power, but also a huge amount of danger. It's easy to forget or mis-remember what a void pointer is and cause all sorts of memory issues and mistakes in your code.

Imagine you wanted something that pointed to a character, then an integer, then a character again. In this case you could use a void pointer with casting, but you still shouldn't. Instead you should look to redesign or figure out a system where you don't need to do that, much like how you should avoid goto or lots of nested if statements.

-1

As everyone already said, void exists simply to specify "nothing". Now why you would want a function that returns nothing is mostly because of good practices. As you get more and more into programming and write bigger and bigger programs, you'll want to keep everything clean, separated and isolate responsabilities. Basically it's the S in the SOLID principles. You may also want to look up DRY just for the hell of it.

Those are things you should only worry about later, but when you get to them, you'll most likely end up with relatively small functions that do only one thing. Now those functions might not have to return anything, for instance an initialization function that only sets up some variables in a class or at the start of a program. This is where you'll want to plug void as a return type, so that the compiler does not complain and force you to return useless garbage at the end of the function.

-1

Good testable code is code that isn't full of functions that don't have side effects. Meaning don't use a void return. But there are times where you simply don't have something to return and void covers those times. I discourage people from using a void return because it means you can't unit test the function by observing it's returned value.

-1

In programming you can think of methods like black boxes. You don't really care what goes on inside. You just care that you put something in and then rely on something happening and occasionally getting something out. So what is the return type of a blackbox that squares a number? Probably the same type as the number or maybe a bigger version (int -> long, float -> double). What's the return type of a black box that makes a string lowercase? Pretty obviously a string (ed: assuming string immutability for the anals out there). What's the return type of a black box that displays a string to the user? In this case there's no need for a return type. You're interested in a side effect of the call and not the result of the call. So a basic return type there would be void.

As an interesting aside in C++ cout is absolutely nothing special. It's a regular old object and similarly << is also nothing special. It's simply an overloaded operator. cout << "Hello World."; is identical to something like myOutputStream.OutputString("Hello world."); It's just a shorthand notation. Now trick question. What's the return type of the call OutputString(...) ?

  • Hint:

  • Answer:

-1

you always need to return a value when doing something, it could be a value of nothing or it could be something meaningful.

say you have a beer bottle in your hand and want to sip it. The way you typically do that (in functions):

// check if beer is left in the bottle 1. You need to know if there is beer left in the bottle so you'd function by calling: isBottleEmpty(), and it would return TRUE OR FALSE then 2. if there is beer in it, you're worry free and you drink away by calling: drinkBeer(). You don't care about anything else except drinking so you return nothing, (void).

-1

Not understanding C++ when learning it is normal.