More on Python functions, parameters, values and stuff
Last time around we started chatting about functions: that is, the legos of the Python universe. You always start a function with the word “def” followed by the name of the function, followed by parentheses and a colon. In other words:
def NameOfFunction ():
Sometimes there will something inside the parentheses. If so, that’s your “parameter.” In the world of Python, parameters and arguments are pretty much used interchangeably, but, technically, parameters are the variables and arguments are the values you give those variables.
If I write “height = 6,” then height is the variable and 6 is the value.
Anyway, a parameter sits beside the function and is often integral to the work it does, sort of like the chocolate chips in a cookie batter. To get an idea of how this works, check out the super simple code below. In this case, the name of the function is “theBatter” and the name of the parameter is “chips.” :
def theBatter(chips): return (chips)
All this does is crank out chips. That is, it cranks out whatever string is placed into parentheses next to the name of the function. I say that it “cranks out” but you don’t actually see the chips that it cranked out unless you tell Python to print them.
How to explain this? Let’s say somebody asks you to calculate 2 + 2. You do it in your head but don’t say anything. The answer is “returned” in your head. Then the person asks you to write the answer down. So you do, printing out “4”.
That’s how it works in Python in this case. Once the function runs, it “returns” the answer but the answer remains in Python’s “mind.” You don’t see the answer till you tell it to print.
Now, let’s actually use (or “call,” as they say) the function described above a couple of times and print out the results:
firstVision = theBatter ('Envision this string as filled with chocolate chips and being poured into batter')
secondVision = theBatter ("Not enough chips yet? That's okay. Pour this string of chips into the function as well")
Note that we placed a literal string of characters that discussed chocolate chip cookies between the parentheses. If you run this code, here’s what you should get:
Envision this string as filled with chocolate chips and being poured into batter
Not enough chips yet? That’s okay. Pour this string of chips into the function as well
Okay, now let’s do something moderately harder. Write this into a new program.
pecans1 = 50 pecans2 = 150 chips1 = 60 chips2 = 120
def yumFactor (ingredient1, ingredient2): total = ingredient1 + ingredient2 return total
todaysCookies = yumFactor(pecans1, chips2) tomorrowsCookies = yumFactor (pecans2, chips1)
print ("The yum factor for today's cookies is " + str(todaysCookies)) print ("The yum factor for tomorrow's cookies is " + str(tomorrowsCookies))
So, what’s happening here?
First, we assign some values to some variables. For example, pecans1 is a variable that has a value of 50.
Second, we write a function called yumFactor. It takes two parameters: ingredient1 and ingredient2. All the function really does is add the values of those two parameters together and then give you the sum total.
Third, we “call” the function twice. The first time, we used two arguments: pecans1 and chips2. The second time, we used two other arguments. In both cases, we assign a name to the outcomes. Those names are “todaysCookies” and “tomorrowsCookies.”
Fourth, we print out those outcomes within the larger context of a string. What we actually get in the Shell are the following two sentences:
The yum factor for today’s cookies is 170
The yum factor for tomorrow’s cookies is 210
(In case you couldn’t tell, I’m a fan of cookies.)
Here’s how all the code looks in IDLE before we run it:
And here’s what prints out in your Shell:
PS: Sometimes I think of functions as dogs and parameters as dog biscuits (you can see that I tend to be drawn to food-related metaphors). You “call” the dog and give her some nice parameters to work on. She gobbles them up, “passing” them right through the body, building strong bones and teeth, as the commercials say. Of course, if we take the metaphor too far, I guess it means that our functions ultimately pass stuff that we have to pick up with plastic bags in the park. Metaphors are dangerous that way.
PPS: If you’re looking for a more sensible tutorial on functions, here’s one you might try.
Featured image from Andrew6111. Wikimedia Commons. https://commons.wikimedia.org/wiki/File:Plate_of_chocolate_chip_cookies,_ready_to_munch.jpg