Whats the purpose of return?

I really don’t get the purpose of return

It hands the result of a function to the caller

print two_plus_two
if our function returns the value 2+2 then the value ‘4’ will be printed to the screen.
If our function didn’t RETURN 2+2 then your function would just silently compute the equation and exit the function handing you back nothing.

This is the basic idea of return. Different languages have different flavors of return. E.G. in ruby the last line of your function is an implicit return
I could simply write

def two_plus_two
  2+2
end

and ruby implicitly RETURNS 4

Python is very similar, the default implicit return is NONE

1 Like