How to Use `return`?

How can we type return True or return False into the code? Can you please show an example?

1 Like

Hi @alqamlas,

You use return to exit from a function with a certain value:

def add_numbers():
  print(1 + 1)
  return(2 + 2) # the function exits here
  print(3 + 3) # this line never gets run, the function stops at `return`

add_numbers() # result: 4

It’s the same if you’re working with booleans:

def is_big_number(n):
  if n > 100:
    return(True) # if the condition matches, the function exits here
    print("The number is bigger than 100") # not run
  else:
    return(False) # if the first condition didn't match, the function exits here
    print("Number is smaller than 100") # not run

is_big_number(58) # result: False
1 Like

Hi, zystvan
I have checked your code in the script window:
def add_numbers():
print(1 + 1)
return(2 + 2) # the function exits here
print(3 + 3) # this line never gets run, the function stops at return

add_numbers() # result: 4

and the result is 2. Is it a mistake sowhere?
I am not sure, that I completely understand python syntax and logic

no, that is correct. 2 is only the printed result, coming from this line:

print(1 + 1)

What did you expect the output would be?

I have expected 4 at least.
According to this:
return(2 + 2) # the function exits here
add_numbers() # result: 4

return is passing data back to the function call.

True, the result is 4. But you don’t do anything with this result, so why should it be displayed/printed?

I think I undertand now, I should type “print add_numbers()” to get 4 on the screen
Until I dudnt use print, I will see only 2 on my screen

yes, because print will display/print output

return just allows you to pass data

thanks for explanation!