[Tutor] Question about Functions
Alan Gauld
alan.gauld at btinternet.com
Wed Sep 11 01:52:59 CEST 2013
On 10/09/13 18:49, novo shot wrote:
> Dear tutors:
>
> The following is an example I found in the Raspberry Pi for Dummies book:
>
> #function test
>
> def theFunction(message):
> print "I don't get ", message
> return "ARRRGH!"
All the code above does is define the function and assign it the name
theFunction (which is a terrible name but we'll ignore that for now!)
It does not execute the function.
> theFunction("this")
Any time the function name is followed by parentheses (but not
preceded by def) the function gets executed. So this calls the
function passing in the argument 'this' which results in the
string "I don't get this" being printed. The value 'AARGH!'
is returned but not stored anywhere so is lost.
> result=theFunction("this either")
This time the function is executed with the argument 'this either'
passed in and the string "I don't get this either" is printed. Again the
value 'AARGH!' is returned but this time it is assigned to the variable
result.
> print "reply is: ", result
This prints the string "reply is: AARGH!"
> The result of this code looks like this:
>
> I don't get this
> I don't get this either
> reply is: ARRRGH!
Which is what I described above.
> When I declare a variable to be equal as the fucntion
> (result=theFunction("this either")) is Python also executing the
> function?
Yes, every time the function name is followed by parentheses
it gets executed (and the return value assigned to any variable
ready to receive it).
> The way I see it, I only called for the function once before printing
> ARRRGH!! Then after that I declared a variable and then I print.
Wrong. You defined the function once. You called it twice.
Then you printed the return value of the second invocation.
> This is how I expected the result to look like:
>
> I don't get this
> reply is: I don't get this either
> ARRRGH!
The 'I don't get...' line is printed inside the function.
The program outside the function knows nothing about that.
It has no way to access that string. That separation of
what's inside the function from the code outside is a
very important feature of programming and is known as
abstraction, data hiding and encapsulation. (All subtly
different variations on the same theme but very important
in programming). It is this feature that enables us to
write reusable functions that can be inserted into any
program without relying on, or breaking, the surrounding
code.
One final thing to note is that when you are at the
Python interpreter prompt the return value of a function
is always printed. But when executing a script file the
return value is not printed unless the program does it
explicitly using print.
>>> def f():
... print 'this is always printed'
... return "this isn't"
...
>>> f()
this is always printed
"this isn't"
Now if you put the definition of f() in a file called
test.py like this:
################
def f():
print 'this is always printed'
return "this isn't"
f()
##############
and run it, you will only see the first line printed.
The return value has been lost because it was never
explicitly printed out. This difference in behaviour
between the >>>> prompt and executing a script often
confuses beginners but is a useful feature when
testing/experimenting at the >>> prompt. You just
need to remember that it will not always give
identical behaviour to a script.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list