[Tutor] i dont understand [parentheses for functions]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Aug 2 19:53:44 CEST 2004



On Mon, 2 Aug 2004, David Dodds wrote:

> hey all im a new computer user and i would love to get into programing
> but i dont understand it


Hi David,

No problem; feel free to ask questions here about the stuff you don't
understand, and we'll do our best to point you in the right direction.


> for example why u have to use "?.() and stuff for.

Hmmm... can you show us an example of what you mean?  The problem is that
parentheses are used in a lot of different contexts; it would be great if
we could focus on a real, concrete example.


One example where parentheses are used is to "fire" off or "apply" a
function.  We can tell Python:

    "To sing a song:

        Sing a verse,
        Sing a refrain.
        Sing another verse.
        Sing the refrain again.
    "



For example:

###
>>> def singSong():
...     singVerseOne()
...     singRefrain()
...     singVerseTwo()
...     singRefrain()
...
>>>
>>> def singVerseOne():
...     print """
... Theory girl,
... Workign in an academic world
... I bet she never had a systems guy
... Bet her advisor never told her why
... I'm gonna try for a..."""
...
>>>
>>> def singRefrain():
...     print """
... ... theory girl,
... She doesn't want to code in C or Perl
... She never touches keyboard, mouse, or screen
... Because she uses an abstract machine
... It's nice and clean"""
...
>>>
>>> def singVerseTwo():
...     print """
... And when she quotes
... algorithms from memory
... You'll find she knows
... all of Knuth Volumes One, Two, Three
...
... Why is the math so tough?
... How can I
... prove my love for a..."""
...
###


When we ask Python what "singSong" is, it tells us:

###
>>> singSong
<function singSong at 0x402c9ed4>
###

that it's a "function".  But how do we turn it on?


Functions are things that can be activated by using parentheses:

###
>>> singSong()

Theory girl,
Workign in an academic world
I bet she never had a systems guy
Bet her advisor never told her why
I'm gonna try for a...

... theory girl,
She doesn't want to code in C or Perl
She never touches keyboard, mouse, or screen
Because she uses an abstract machine
It's nice and clean

And when she quotes
algorithms from memory
You'll find she knows
all of Knuth Volumes One, Two, Three

Why is the math so tough?
How can I
prove my love for a...

... theory girl,
She doesn't want to code in C or Perl
She never touches keyboard, mouse, or screen
Because she uses an abstract machine
It's nice and clean
###


There it goes.


If we leave off the parentheses, a function won't fire off.


Also, if we write "singSong()" like this:

###
>>> def singSong():
...     singVerseOne
...     singRefrain
...     singVerseTwo
...
###

then when we fire off singSong, nothing will appear to happen,

###
>>> singSong()
>>>
###

because although the 'singSong' function itself fires off, it doesn't tell
Python to fire off the other three functions.


So parentheses are used to fire off, or "apply" functions.



It seems a little redundant to use parentheses to fire off functions, but
it makes more sense when functions "take in" things.  For example:

###
>>> def sayMadLib(verb1, noun1):
...     print "Once upon a", noun1,
...     print "there was a cow who would", verb1, "all day"
...
>>> sayMadLib("blue moon", "croak")
Once upon a croak there was a cow who would blue moon all day
###


Whoops!  Got things backwards.  Let me try that again:

###
>>> sayMadLib("croak", "blue moon")
Once upon a blue moon there was a cow who would croak all day
###

That's better.


What we have here is a function that takes in two things.  Those two
"parameters" go in the parentheses, so that Python knows to give them to
the function.  The parentheses are there to make it easier to see that a
function is "taking in" things when it is being called.


Does this make sense so far?



More information about the Tutor mailing list