Why do we need to return true / false inside the function?


#1

Sorry kind of a meta question here I guess...just trying to wrap my brain around the concept.

Codecademy says "&& evaluates to true when both statements are true" and "|| evaluates to true when either or both are true"

Okay, makes sense.

But if that's the case, why do we have to write an "if" statement inside the function telling the computer to return true when both are true (in the case of &&), or false when one is not true? And why do we need to write an "if" statement telling the computer to return true when one or both are true (in the case of ||)? If we already defined the variables as either true or false at the beginning (outside the function) shouldn't it then be a given that && || etc. will evaluate a certain way?

Does this question make sense? Or am I just not getting it?


#2

You could have values changing in different circumstances.

Such as a player having enough currency to buy a weapon, or being a certain level. :slightly_smiling:


#3

Sorry, I don't totally understand this. Can you elaborate a bit?


#4

Pretend you are playing Minecraft.

There is a value telling Yes (True) or No (False) if you are in creative mode.

Also there is a Yes (True) or No (False) if you are in Peaceful mode.

If both conditions ( && ) are true, then there will be no hostile mob spawn, and no player tracking.


#5

sorry...i've never played minecraft :flushed:

i'm not really a gamer

still not really picking up what you're putting down. i mean, it makes sense - if both conditions are true, this particular thing will happen, if not, something else will happen (right?). I just don't get why you need to write an "if" statement to make that happen if && already means "true if both conditions are true."


#6

You wouldn't know (more like your program wouldn't know) unless you had a way to check the conditions, which is with an if-statement. :slightly_smiling: