-2

I'm struggling to grasp the importance of the return statement.

Can someone in layman terms explain it to me? Especially the power behind it.

I have read definitions, seen examples, and understand how it works. But I also read that it extremely powerful and important, and that it is only used in functions (or methods if within a class).

Can anyone offer an in-depth explanation?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
xmx
  • 137
  • 2
  • 14
  • 1
    Functions allow you to perform computations and execute algorithms. Sometimes, you want to know what the result of an algorithm was. Thus, inside your function, you `return` the result and then outside code has access to it. – gyre Apr 21 '17 at 05:56
  • 1
    `return` a calculated value... There. Layman terms – OneCricketeer Apr 21 '17 at 06:04

1 Answers1

1

Everything is an object in python, even functions. A function can be assigned to a variable, passed to another function and can be returned from another function.

return statment helps a function to return a value to the calling statement. The reason why it is powerful is because variables in python can be used to return functions from other functions.

This allows it to perform some interesting things like decoupling functions using decorators.

To know about Decorators read this

Naveen Dennis
  • 1,223
  • 3
  • 24
  • 39
  • It don't think the question was asking about returning function objects – OneCricketeer Apr 21 '17 at 06:10
  • Thanks- I'm new to python. Lots of this is above my head- thus trying to seek a more "dumbed" down description- like your second paragraph. – xmx Apr 21 '17 at 06:22