ELI5. What does “return” do in programming? I read about it a lot and still dont understand the purpose.

344 views

ELI5. What does “return” do in programming? I read about it a lot and still dont understand the purpose.

In: 0

10 Answers

Anonymous 0 Comments

As with all things related to programming, it depends on the language. It could conceivably do anything.

However, in most languages, including C, Java, Basic, and Python, the `return` statement causes the program to exit the current function or subroutine and optionally report a value.

E.g. if I wrote a function called `add_one(x)` as follows:

def add_one(x):
y = x + 1
return y

Then when I call the function, e.g. `add_one(10)` it outputs the value of `y`, in this case 11.

You are viewing 1 out of 10 answers, click here to view all answers.