Go to Chapter 41 in A Smarter Way.
A function in Python is like a function in math: you put something in, it gets transformed, and you get something out.
There are two stages to using a function, defining it and calling it.
To define a function:
def add_two_numbers(): first_number = 1 second_number = 2 total = first_number + second_number print(total)
Then we could call it like this:
add_two_numbers( )
But this gives us fixed values for the numbers.
Go to Chapter 42 in A Smarter Way.
To define a function that accepts input values:
def add_two_numbers(first_number, second_number): total = first_number + second_number print(total) add_two_numbers(2, 3)
Because first_number and second_number aren’t assigned any values in the function def parameters, they are positional parameters. You’ll access their values using the names you give them within the parentheses.
See Chapters 43 and 44 in A Smarter Way.
To create a function that accepts input values, but also has default values for the inputs:
def call_cat(summons="Here, ", cat_name="Niko"): print(summons + cat_name) call_cat()
Because these parameters are defined as key/value pairs, they are keyword parameters. You can access and change the values by using the keys as indexes.
Call this function and supply different values than the defaults:
call_cat("Hey, stinker ", "Betty")
Go to Chapter 45 in A Smarter Way.
You can mix positional and keyword parameters, as long as you list them in the right order.
Positional parameters come first.
Keyword parameters without default values come second.
Keyword parameters with default values come last.
def describe_cat(cat_name, cat_color="", eye_color="green"): print(cat_name, cat_color, eye_color) describe_cat("Betty")
Go to Chapter 46 in A Smarter Way.
Functions can have an unknown number of arguments (this allows for future modifications).
You’d still call the function using keywords:
def describe_cat(cat_name, cat_color="", cat_eyes_color="green", **other_cat_details): print("My cat " + cat_name + " has " + cat_color + " fur and " + cat_eyes_color + " eyes.") for key, value in other_cat_details.items(): print(key + ": " + value) describe_cat("Betty", cat_color="gray", cat_eyes_color="blue", coat="tabby", spayed="true")
The extra values are held in a dictionary named for the double-asterisk parameter, in this case other_cat_details.
Go to Chapter 47 in A Smarter Way.
To return values from a function:
def add_two_numbers(first_number, second_number): total = first_number + second_number return total total_of_two_numbers = add_two_numbers(2, 3) print(total_of_two_numbers)
Obviously, you need to capture that returned value in a variable, in most cases.
Go to Chapter 48 in A Smarter Way.
To use the return values from a function as variables or arguments:
print(add_two_numbers(2, 3))
Go to Chapter 49 in A Smarter Way.
A global variable is defined in the main body of your code:
x = 1
A local variable is defined inside a function:
def set_x(): x=2
Try the example on page 149 to see how this works.
Exercises
See http://www.asmarterwaytolearn.com/python/44.html
and continue through chapter 49.
- In examples.py, define a new function.
- Use at least one positional param, one keyword param with no default value, and one keyword param with a default value.
- Call the function and print its output.