#Need help understanding differences in code

1 messages · Page 1 of 1 (latest)

plain fable
#

I'm very new so bare with me please, just starting to learn Godot and the classes online I'm taking don't have a spot where I can ask questions to anyone only an AI bot. I'm struggling to figure out what the differences in these two codes are and why you would want one over the other. Thanks.

Challenge: Create a function called _has_won.
This function has a parameter called score.
It needs to return true or false for whether or not the score is above 100.

My attempt:

var score : int = 100

func _ready():
_has_won (score)

func _has_won(score):
if score >= 100:
print("You Win")

The way I was supposed to write it:

func _ready():
var game_over = _has_won(120)
print(game_over)

func _has_won(score):
if score>=100:
return true
else:
return false

stable cosmos
#

The function _has_won(score) is answering a question, that's it's sole responsibility. Your version doesn't answer a question, it prints a response so a name like _show_win_message(score) would be more appropriate.

plain fable
#

so return true/return false is answering a questions but print is not if i understand correctly? The return is giving the answer if I'm understanding you correctly.

stable cosmos
#

That's right. In an ideal program, there would be functions that answers questions and functions that "do stuff". _has_won(score) takes care of what the score means so you don't have to think about it elsewhere.

plain fable
#

thank you so much

stable cosmos
#

No problem!

candid osprey
#

can think of it too that return, returns the outcome of the function to the variable.

var game_over = _has_won(250)

the function will run and return the outcome to game_over variable.

returns are good for any number of types not just bools.

and functions that just do stuff as other user was saying those are called void function. they don’t return anything.