A Quick Peek at Making Our Own Functions

Functions

What's a function (again)?

A function is a named sequence of statements that performs a specific task or useful operation

Built-In Functions

What are some built-in function that we just learned about?

Our Very Own Functions!

Motivation for Creating Functions

Why would the ability to create our own functions be useful?

Defining a Function

def the_name_of_your_function(parameter_1, paramter_2):
	""" some code """
	print("I'm doing something useful!")

Defining a Function Continued

  1. a function definition always starts with the reserved word, def (the function header)
  2. … followed by the name of your function
    • (function names adhere to the same rules as variable names)
  3. … followed by parentheses
  4. with an optional, comma separated list of inputs enclosed in parentheses (depends on function)
    • if the parentheses are empty, then the function is being called without arguments
      • no_inputs_needed()
  5. lastly, an indented block of code (the function body)
    • this is just one level of indentation in →

What's This About an Indented Block?

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")

Let's Try Creating a Function!

Write a function called say_multilingual_meow:

def say_multilingual_meow():
	japanese_meow = "nyan"
	english_meow = "meow"
	print(japanese_meow)
	print(english_meow)

Calling Your Function

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.

Let's See How This All Works!

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!

Calling Your Function Continued

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!")

Just the Definitions, Please!

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.)

Just the Calls, Please!

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.

Multiple Function Definitions

You can define more than one function in your program!

def my_first_function():
	print("first")

def my_second_function():
	print("second")

Functions in Functions

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

Functions in Functions Continued

Notice that in the previous code…

def say_moo():
	print("moo")

def main():
	say_moo()
	say_moo()

main()

"Local" Variables

"Local" Variables Continued

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.

"Local" Variables Continued Some More

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)

Defining a Main Function

def main():
	print("in main function")

main()

Main Function Continued

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()

(Totally Optional) A Whirlwind Tour of Conditionals