[Tutor] thesaurus

Alan Gauld alan.gauld at btinternet.com
Sun Jul 12 10:00:05 CEST 2009


"Pete Froslie" <froslie at gmail.com> wrote

> This seems the basic form for a function:
>
> *def** hello*():
>>     *print* "Hello World!"
>>     *return*

That is a very limited form that encourages bad practice. The more
general form is:

def aFunction(inputValue1, inputValue2,...):
      # do some processing here
      return output_value1, outputValue2....

By adding the input and output values we make the function
independant of its immediate surroundings and therefore
more easily reusable across different programs.

We can  then call it like:

x,y = aFunction(a,b)

>> I assume each part that can be separated will be done so in this format 
>> at
> the start of the code; consequently, allowing me to call 'hello' later 
> when
> I need it. Does this also mean that I will be able to call those 
> functions
> separately later when I import 'thesaurus.py' into a new code also?

Exactly. The first benefit of functions is in encapsulating parts of your
program and providing structure which makles it easier to read but
the real power of functions is in making those blocks of code available
to other programs too.

Read my tutorial topic on Moduules and Functions more information.

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




More information about the Tutor mailing list