What's a function (again)? →
A function is a named sequence of statements that performs a specific task or useful operation
What are some built-in function that we just learned about? →
Why would the ability to create our own functions be useful? →
def the_name_of_your_function(parameter_1, paramter_2):
""" some code """
print("I'm doing something useful!")
def the_name_of_your_function(parameter_1, paramter_2):
""" inside the function """
print("something")
print("another thing")
""" outside of the function """
print("and another thing")
Write a function called say_multilingual_meow: →
def say_multilingual_meow():
japanese_meow = "nyan"
english_meow = "meow"
print(japanese_meow)
print(english_meow)
So… now that you have a shiny new function, how would you call it? How many arguments would it take? 0, 1, or 2? →
def say_multilingual_meow():
japanese_meow = "nyan"
english_meow = "meow"
print(japanese_meow)
print(english_meow)
"""
Once the function is defined in your file, you can call it!
"""
say_multilingual_meow()
It takes 0 arguments.
In one file, write the function definition and function call from the previous slides.
def say_multilingual_meow():
japanese_meow = "nyan"
english_meow = "meow"
print(japanese_meow)
print(english_meow)
say_multilingual_meow()
print("done!")
nyan
meow
done!
So… what actually happened there? →
def say_multilingual_meow():
japanese_meow = "nyan"
english_meow = "meow"
print(japanese_meow)
print(english_meow)
say_multilingual_meow()
print("done!")
Let's look at the same program, but without the function call. What do you think the output of this program will be? →
def say_multilingual_meow():
japanese_meow = "nyan"
english_meow = "meow"
print(japanese_meow)
print(english_meow)
(That's supposed to be nothing! A function definition alone won't execute the code in a function.)
What about this version of the program? What do you think the output will be? →
say_multilingual_meow()
We get a NameError!
Traceback (most recent call last):
File "/tmp/foo.py", line 1, in <module>
say_multilingual_meow()
NameError: name 'say_multilingual_meow' is not defined
This may be obvious, but a function has to be defined before it can be used.
You can define more than one function in your program!
def my_first_function():
print("first")
def my_second_function():
print("second")
In fact, you can use a function that you've already defined in another function that you're creating. What do you think the output of this program will be? →
def say_moo():
print("moo")
def main():
say_moo()
say_moo()
main()
moo
moo
Notice that in the previous code…
def say_moo():
print("moo")
def main():
say_moo()
say_moo()
main()
def my_first_function():
a = 1
print(a)
def my_second_function():
b = 2
print(b)
The variables a and b are local to my_first_function and my_second_function respectively.
Because variables defined within a function are local
def my_first_function():
a = 1
print(a)
def my_second_function():
a = 200
print(a)
(calling both functions consecutively prints out 1, then 200)
def main():
print("in main function")
main()
For example, these two programs are equivalent:
print("About to do stuff")
print("Doing stuff")
print("Done")
def main():
print("About to do stuff")
print("Doing stuff")
print("Done")
main()