# On Functions and Booleans

Hey guys, there seems to be some confusion about functions, returning, and booleans. I’m going to try to help alleviate some of the confusion.

First of all, think of booleans like a yes or no. The possible states of a boolean are only two. Those states are true, and false. When someone tells you something, in english, that statement (if it’s not a question) is either true, or false. That’s all a boolean is. It is just a syntax term which tells the compiler to make sure that variable cannot hold any other state than true or false. For example, a boolean cannot hold the value “Bulls and Cows”;

Int he video, he says false is equivalent to 0. This is true. The state of false, for a boolean, is represented as a 0. He also says that true is equivalent to 1. This is also partially true. Technically speaking, true is anything that isn’t 0. For instance, the number 34 is true. Although a boolean type can only have two states, you can evaluate things to a boolean that are not booleans in the manner I just described. If it evaluates to zero, it is false, anything else is true.

If the above paragraph is confusing, ignore it, just remember, that a boolean can only be true or false, and you’ll be fine.

A function is like a machine in a factory. You put some raw materials on a conveyor belt (parameters), it goes into the machine(function) and the product comes out the other end (return). You do not have to use this return if you don’t want to, but if the return is not void, you must return something, and it must be the type that you declared the function as. Here is what a prototype might look like of our analogy.

Product Machine(Raw Materials);
bool AskToPlayGain(); <- Here the product is a boolean, the machine is AskToPlayGame, and we don’t have any raw materials.

Now let’s combine the two. Any condition evaluates to a boolean. for example return 1 == 1; return true, because 1 does equal 1. return 1; also returns true, because 1 is true. return true; also returns true, because true is true;

Now how does this applies to the lesson? We are asking the user if they want to play again. If they say yes, they want to play again, so the state we want to return is true. If they say no, we want to return false because they do not want to play again.

As a matter of pet peeves, the function name is probably not great. Generally, with things that return a boolean, they should be questions or states. So a better name would be bool WantsToPlayAgain(); and it would be true or false. An example of a state would be bool isBoy; This would be true if someone was a boy, and false if not. bool AskToPlayAgain doesn’t imply the return of a boolean. I do advise you leave it alone and follow the video though so you don’t get lost later.

I’ll be happy to answer any other questions you guys might have. I won’t paste code because that is not how you learn. I will answer questions though if you have them.

1 Like