On 14/05/13 00:01, Daniel Magruder wrote:

I am still confused as what return does.

Having seen the other replies I'll try a slightly
different explanation.

All programs are formed by a hierarchy of functions.
You start with a top level driver block which then utilizes or calls helper functions. Those helper functions, in turn, call other helper functions. We can show it diagrammatically as a tree and several software design notations exist that do this. Here is one example:

http://www.smartdraw.com/resources/tutorials/Entity-Structure-Diagrams

Since this is a text forum I'll do it in a different format...

Driver block
   helper1
      funcA
      funcB
   helper2
      funcA
      funcC
   helper3

In this hierarchy the Driver calls 3 functions.
The helper functions call 2,2 and zero functions respectively.
funcA is called by both helper1 and helper2. When a function calls another function it effectively pauses its own execution and hands control to the called function.

Inside each of these 6 functions there are return statements(*) the purpose of which is to return control to the next higher level in the hierarchy. The function stops as soon as it reaches a return statement and control reverts to the previous level.

Thus in funcA the first time it is called it will return to helper1.
The second time it is called it will return control to helper2.

return also has a secondary purpose which is to pass back a value to the caller which value is the result of the functions calculations.

(*) Some functions do not have an explicit return statement. In these cases they run to completion and control then reverts to the calling function with an implied return value of None.

Does that help?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to