[Tutor] Equivalent of a Subroutine in Python?
Michael P. Reilly
arcege@shore.net
Thu, 1 Feb 2001 13:15:20 -0500 (EST)
>
> This message is in MIME format. Since your mail reader does not understand
> this format, some or all of this message may not be legible.
>
> ------_=_NextPart_001_01C08BD2.E6AD3D40
> Content-Type: text/plain;
> charset="iso-8859-1"
>
> Hello,
>
> Another question from someone totally new to Python. Is there an equivalent
> in Python to a sub-routine, (e.g. gosub and return). I want to create a
> modular program with sub-routines to perform distinct tasks wihin the
> program for organizational and debugging purposes, etc. Is the only (or
> best) way to do this is with modules? A function works but the values
> obtained within the function do not appear to be valid outside of that
> function. I guess I am looking for the best approach to create the
> subroutines for execution from the main flow of the program.
There sure are. You would either use the "def" statement or the
"lambda" expression to create a subroutine/procedure/function.
>>> def WhatsMyLine(name, message):
... print name, 'said', repr(message)
...
>>> WhatsMyLine('Arcege', "Spam, Spam, Eggs and Spam")
Arcege said 'Spam, Spam, Eggs and Spam'
>>> is_odd = lambda n: (n % 2) == 1
>>> is_odd(3)
1
>>> is_odd(4), is_odd(5)
(0, 1)
>>>
I would suggest that you read the Python tutorial; it could help you a
lot with your questions.
<URL: http://www.python.org/doc/current/tut/tut.html>
-Arcege
--
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager | Email: arcege@shore.net |
| Salem, Mass. USA 01970 | |
------------------------------------------------------------------------