[Tutor] functions in Python

Steve Nelson sanelson at gmail.com
Mon Apr 17 18:42:05 CEST 2006


Sorry - including list.

On 4/17/06, Payal Rathod <payal-python at scriptkitchen.com> wrote:
> what is the difference between,
>
> def func():
>         ....
>
> and
>
> def func(x):
>         ....

When you define a function, you are writing a block of code which you
can ask to perform a task.  The task may be simple, and not require
any additional information, or it may be more complex and need
information.

Take a simple analogy.  Let's suppose I gave you the instruction
"Switch off the light" and there was only  one light, you wouldn't
need any more information.  We could call that:

def toggleLight():

Now let's take a more complicated examle - making tea.  If I give you
the instruction "Make me a cup of tea" you might ask questions like
"Milk? Sugar? Kind of tea?"  I need to give you this information
before you can perform the task.  Similarly in a function - you may
need to pass information (arguments) to the function.  Here's the tea
example:

def makeTea(sugar, milk, kind):

We might call the function as follows:

makeTea(2, "none", "darjeeling")

The internals of the function would be able to parse the arguments you
gave it, and carry out the task you've asked it to do.

As to when to use which, I think you'll find that most of the
functions you write will require information if they're to be of much
use.  Only a very simple function doesn't take any arguments.

Hope that helps :o)

S.
>
> When to use which? (please do not say "returns a value" for I do not understand the meaning
> of the same)
>
> With warm regards,
> -Payal
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list