[Tutor] functions in Python

Robert Schumann robert at cantab.net
Mon Apr 17 18:10:51 CEST 2006

Payal Rathod wrote:
> Hi,
> I am now reading Alan's tut on Python, before that I have read a few other tuts too.
> But I am not getting functions exactly. I mean what is the difference between,
> def func():
>         ....
> and
> def func(x):
>         ....
> When to use which? (please do not say "returns a value" for I do not understand the meaning
> of the same)
It's a bit like the grammar of regular language.

You could say "I kick" (which is like func(), because you're not 
specifying an object to operate on) or your could say "I kick the ball" 
(in which case x = "the ball").

The grammar of functions is very specific.  In a sentence you can string 
together quite a few "addons":
I kick - the ball - hard - down the hill - to Peter.
But in a function in most programming languages, you have to give the 
same number of objects that it is asking for e.g.
   def kick(what, how, where, ToWhom):
If any of them are missing, the function is going to complain, although 
as you learn more about Python you'll find ways around this.  If I had 
defined my function grammar as
   def kick():
you wouldn't be allowed to tell me *any* of that extra information when 
you call the function.

The return value of the function is the effect it has - for example, 
assuming the sentence is true, "I kick the ball to Peter" has two 
effects: first, the ball is now with Peter and second, the listener 
knows this.

OK, the analogy is not that great....


> With warm regards,
> -Payal
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list