> Date: Tue, 10 Sep 2013 13:34:50 -0400 > From: novo shot <novos...@gmail.com> > To: tutor@python.org > Subject: [Tutor] Question about Functions > Message-ID: > <CAGk5SVGPVL-1xgEukXCcRUmmYJG_Aq+mh+wnR=ot5ajbcht...@mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > 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!" > > theFunction("this") > > result=theFunction("this either") > print "reply is: ", result > > --------------------------------------------- > The result of this code looks like this: > > I don't get this > I don't get this either > reply is: ARRRGH! > > ---------------------------------------------- > Here's what I don't understand: > > When I declare a variable to be equal as the fucntion > (result=theFunction("this either")) is Python also executing the function? > > 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. > > This is how I expected the result to look like: > > I don't get this > reply is: I don't get this either > ARRRGH! > > ----------------------------------------- > > Can you help me understand? I can't move forward until I understand how > Python solves this code. > > Thanks in advance
Function in Python work very much the same way like they do in most programming languages. When working with them you can't really "feel" the difference. There is a big difference, however, "underneath" (just always keep in mind everything in Python is an object, the book should mention that repeatedly). Since you didn't mention I have to assume you didn't do any programming before python. In the case of big C's (C, C++, C#) I was always thought that I should look at a function as a variable. In particular the exact same variable you return. I.e. if I have a function: >>> def add1(int_number): return number+1 you can look at function add1 as if it's an integer because it returns an integer. The same applies to your example, which you can see if you do some introspection: >>> type(result) <type 'str'> So you see your result is nothing more then a string! Basically whatever you return in your function will be assigned to the variable you return it to. Because you return "AARGH" in your function and assign the return value to result: >>> result=theFunction("thisEither") the variable result will become "AARGH". So this line basically amounts to: >>> result= "AARGH" This is a special case because this function always returns the same thing. That's not usually the case with functions. I.e. let's return to my add1 function. Output (that part behind the return statement) of add1 function will change depending on the input number: >>> add1(3) 4 >>> added = add1(5) >>> print added 6 What you can also see from the above example is that when I explicitly assigned a variable to a function [added = add1(5)] the result of the function did not print out, but the function must have executed because variable added has a value of '6'. So function executes every time you call on it, but prints out value of "return" only when you don't specify a "container" that will hold it's output. I'm pretty sure you're confused here because you have a print statement in your function. Print statement calls on your standard output that works "over" anything and pretty much "whenever", it's kind of special that way. If I changed my example function add1 to: >>> def add1(int_number): print "Working, hold your horses...." return number+1 then my output from before would be: >>> add1(3) Working, hold your horses.... 4 >>> added = add1(5) Working, hold your horses.... >>> print added 6 This is the main difference between return and print, think of return like it defaults to print if there is no value to which it can assign what it returned. That is also why you always see line "I don't get (this/this either)" printed every time you call on your function. >>> theFunction("this") I don't get this ARRGHHH >>> result=theFunction("this either") I don't get this either >>> print "reply is: ", result reply is AARGGGHHHH I think it should be pretty clear by now how it works..... On another note apparently ugly with an f in front is a bad word around here, my apologies I'm fairly new around these places and was inquiring for your help not even a week ago and don't really know how things work. But I am willing to help out like you did me, does that count? Also I'm not a programmer so I imagine analogies when I program and may be off point sometimes, hope I didn't make too many people cringe because of that... Regards, Dino _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor