We worked through several examples of functions, talked about global & local variables, print vs return. I've attached the code snips that we looked at in class, including the (very tricky and outside the depth of an intro class) idea of "closures".
For a good time, run "closure.py" with the online "Visualize Python" tool.
From the 2016 class, see these notes (which we went over) and these other notes (which we didn't).
"""
A class example of functions:
the "happy birthday song"
"""
first_line = "hi there"
def happy_birthday(name):
""" Input: a person's name.
Output: the happy birthday song as a string. """
first_line = "Happy Birthday to you"
song = first_line + ",\n" + \
first_line + ",\n" + \
"Happy Birthday dear " + name + ",\n" + \
first_line + "!\n"
return song
def main():
print happy_birthday("Peter")
print
print happy_birthday("Mary")
print
print "first_line is " + first_line
main()
last modified | size | ||
bad.py | Sun Dec 22 2024 10:24 am | 233B | |
closure.py | Sun Dec 22 2024 10:24 am | 165B | |
one.py | Sun Dec 22 2024 10:24 am | 405B | |
returns.py | Sun Dec 22 2024 10:24 am | 197B |