fbpx

Void and bool are simple types that you can use with very little explanation. There are a few details that you should be aware of though.

Think of void as an absence of a type and not really a type itself. There are specific places where void can be used and this will depend on your language.

Most languages allow you to declare a method that returns nothing by declaring the method returns void. When you do this, then it’s a compile error to try returning an actual value.

You cannot use void to declare a variable or a reference but if your language supports pointers like in C++, then you can have a pointer to void.

You also cannot use void as a template type in C++ or as a generic type in C#. That’s all for C#. But C++ has one additional twist to using void with templates. In C++, you can specialize a template. Let’s say you have a template that takes two type parameters. You could instantiate an instance of this template with an int and another int. Or a short and a char. Or any two types. But you can’t use void. There is a way to get around this by specializing the template so that the new specialized template only needs one parameter type and the other one is written to use void. So even though you can’t pass void directly through the type arguments to a template, you can use void in a special version of the template.

You might expect that a data type that can only hold true or false values requires no further explanation. But there wasn’t always a bool data type available so developers sometimes created their own. And these creations didn’t always match.

Most of the time, code that needed to define its own bool data type would define true to be the value 1 and false to be the value 0. A notable exception to this was Visual Basic which defined a type called a VARIANT_BOOL and it used -1 to mean true and 0 to mean false. You can’t just assign true or false to your variable but will need to use special VARIANT_TRUE and VARIANT_FALSE values that have been defined to be -1 and 0.

Listen to the full episode about void and bool types, or you can also read the full transcript below.

Transcript

Episode 7 explains the various data types including void and bool but it was mainly an overview. This episode goes into more detail about void and bool.

Alright, think of void as an absence of a type and not really a type itself. There are specific places where void can be used and this will depend on your language.

Most languages allow you to declare a method that returns nothing by declaring the method returns void. When you do this, then it’s a compile error to try returning an actual value. You have two options. The first is to just let the method end without returning anything. This means you just don’t put a return statement at the end of your method. The second option is used when you want the method to return early and in this case, you just write a return statement. Many return statements have a variable or some other value after the return statement. But when you have a return with nothing after it, that allows you to return from a method at that point without returning anything to the caller.

Some languages like the C language use void in the parameters of a method to mean the method requires no arguments to call the method. Other languages like C++ and C# just use empty parenthesis after the method name to mean there are no parameters.

You cannot use void to declare a variable or a reference but if your language supports pointers like in C++, then you can have a pointer to void. This is useful especially for methods that allocate memory where the memory will be used for some unknown purpose. At least the method that allocates the memory has no idea what the memory will be used for. If you call such a method to get some memory, then you should have some purpose in mind for the memory. Once you get the memory, then you can cast the void pointer to a pointer to a specific type.

You also cannot use void as a template type in C++ or as a generic type in C#. That’s all for C#. But C++ has one additional twist to using void with templates. This is another example of how much more powerful C++ templates are compared to C# generics. In C++, you can specialize a template. Let’s say you have a template that takes two type parameters. You could instantiate an instance of this template with an int and another int. Or a short and a char. Or any two types. But you can’t use void. There is a way to get around this by specializing the template so that the new specialized template only needs one parameter type and the other one is written to use void. So even though you can’t pass void directly through the type arguments to a template, you can use void in a special version of the template. Make sure to listen to episodes 57 and 58 to learn more about templates and generics.

I’ll explain the bool type right after this message from our sponsor.

( Message from Sponsor )

You might expect that a data type that can only hold true or false values requires no further explanation. But there wasn’t always a bool data type available so developers sometimes created their own. And these creations didn’t always match.

The first thing we need is some memory to hold the true or false value. How much memory do we need? The absolute smallest amount of memory needed is just a single bit. But we can’t address a bit in memory by itself. The smallest amount of memory we can address is a byte. Because of this, a bool will occupy a whole byte all by itself.

This assumes that the bool variable needs to live in memory. The compiler might be able to put the variable in a register instead inside the processor.

If you want to have multiple bools share a single byte, then you’ll have to either write a class for this yourself or use something like a bitset class that’s designed to pack multiple bits together.

The second thing we need is to decide what value will represent true and what value will represent false. You don’t need to worry about this for modern languages that have native support for a bool data type such as both C++ and C#. But you do still need to be aware of the issue in case you ever need to write code that communicates with older code.

Most of the time, code that needed to define its own bool data type would define true to be the value 1 and false to be the value 0. A notable exception to this was Visual Basic which defined a type called a VARIANT_BOOL and it used -1 to mean true and 0 to mean false.

This seems strange to think of true as -1 but remember how -1 is represented in two’s complement as all 1’s. Visual Basic chose to use -1 because it used bitwise operators instead of logical operators and would examine each bit. If true were to be defined as the value 1, then Visual Basic would return the wrong result because it would see the 1 while all the other bits would be 0. If your code ever needs to interact with scripting components, then you’ll probably need to follow the VARIANT_BOOL way of doing things. You can’t just assign true or false to your variable but will need to use special VARIANT_TRUE and VARIANT_FALSE values that have been defined to be -1 and 0.

Now that you know how bool variables are stored in memory, the next question is how do you use them. All the control flow statements and loop expressions need to evaluate to a true or false value. This is needed to know if the body of the if statement should be executed or skipped or if the body of the loop should be run again or not.

When you have a bool variable directly, then this is easy. Let’s say you have a variable called continuePlaying that will be true as long as the user wants to keep playing a game and will be false when the user wants to quit the game. You could then have a while statement that would keep looping through your code as long as the continuePlaying variable is true.

But how do you write this while statement? Do you say “while (continuePlaying == true)” or just “while (continuePlaying)”? There is a difference because the first version that compares the variable with true calls the operator equals method which returns a bool result. It’s the result of the operator equals method that the while loop sees. The version that uses the continuePlaying variable directly works because the continuePlaying variable is already a bool type that the while statement expects.

What if instead of a bool variable though, we wanted to have a game that could only be played 5 times and then would exit. We could still use the continuePlaying bool variable and then set it to false when a integer variable reaches zero. This assumes that we start the integer variable out at the value 5 and decrement it by one after each game. When the count reaches zero, then we test that and set the continuePlaying variable to false.

But this is a roundabout way of doing things. Sometimes it’s better to just test the int variable directly. This is where some languages take a different approach. In C++, you can just write “while (gamesRemaining)”. You could, if you wanted to, take the more explicit route and write “while (gamesRemaining != 0)”. What’s the difference between these two approaches? I’ll start with the second one. It calls the operator not equals which returns a bool result that the while statement expects. But how does the first version work? Well, C++ will interpret any non-zero value to be true and and any zero value to be false. So as long as gamesRemaining hasn’t yet reached zero, then this will evaluate to true.

Languages like C# decided to prevent this automatic conversion of non-zero values to be true because it turns out to be a major source of bugs in C++ code. So if you’re using C#, then you’ll have to be explicit and write “while (gamesRemaining != 0)”.

And then there’s one final topic I want to explain about bools in this episode. It’s possible to write your own conversion operators to convert your custom classes to bool. You only want to do this when it’s obvious and expected. Otherwise, it’ll just cause confusion. But if you have a custom class that would benefit from this, then go ahead. You can perform whatever checks or calculations you need inside your operator bool and return either true or false.

Let’s say you have a custom class called TrashCan. It might make sense for a TrashCan to be true if it has trash inside and false if it’s empty. Once you have this in place, then you can use variables of type TrashCan directly in an if statement or in a while loop and know that your custom operator bool will be called to determine if the value is true or false.