Welcome back to Python Basics! If you haven’t worked through parts 1 and 2, go back and make sure you understand those before you work on this part. We will be using several of the concepts we talked about in the previous parts today, so make sure you understand those first.
Today, we are going to be going over functions, aka methods. Here, we will call them functions, but if you’ve worked with methods before that’s the same thing.
Functions are small blocks of code that can be called repeatedly. When you are doing the same thing over and over, functions are super helpful to use so you only have to write the code once. This also helps with debugging so you only have to change it once, not many times all over.
To define a function, use the following syntax.
def example_function(): # function body here
You can use this syntax to define all sorts of functions. For example, let’s write a function that just prints out a statement to confirm it is working.
def example_function(): print("Our function works!")
Now, if you just type this out in your code and run it, nothing will print out. That’s because the program just defines the function, it doesn’t actually call the function. To call a function, you can do one of two things.
First, you can call the function from the Python shell. After you’ve run your code, you can type the name of the function with parentheses after the name like below.
>>> example_function() Our function works!
You can also call a function directly inside your code. To do this, type the following inside your code body.
Now that we know what a function looks like and how to call it, let’s look at how we can use functions.
The first thing that makes functions easy to use in many places is that you can pass in arguments. Arguments are how you pass in information to the function to use within the function body. For example, the following function will print out the value of ‘a’ which you pass in.
def print_argument(a): print("The argument passed in is," a)
If we run it from the shell, it will output the following.
>>> print_argument(5) The argument passed in is 5 >>> print_argument("arg") The argument passed in is arg
Since Python is of variable type, for this function we can pass in whatever type we want for our argument.
Functions take in parameters to use inside the function body, but you can also pass a value back to the caller. To do this, we’ll use a return statement. For example, let’s write a function that adds two numbers together and returns the value back to the user.
def add _nums(a, b): return a + b
Then, we can run this function in the shell and get the following output.
>>> add_nums(3, 10) 13
But what if we run this function in our code body? We can’t run it like we would in the shell because then we would lose the return value of the function. Since the return value is what we really want, we need a place to save the value. To do this, we need to set the function equal to a variable like in the code block below.
result = add_nums(3, 10) print("Result is", result) >>> Result is 13
This is a good overview of the basic syntax of a function. Functions can be as long or short as you want and can have as many arguments as you need. Inside a function you can write any of the structures we talked about in the previous two parts including loops, if statements, and so many others.
Next, let’s take a look at some examples of more complex functions.
First, let’s write a basic function using a for loop. For as many times as the argument passes in, the function should print out “Do it again!”. The function does not need to return anything. The function declaration and a test are written below.
def looping(repeated): # your code here >>> looping(7) Do it again! Do it again! Do it again! Do it again! Do it again! Do it again! Do it again!
Now, let’s write a function that tells a user if an argument is positive, negative, or zero using if statements. I’ve written the declaration of the function below. Your job is to fill in the code body so that it prints “POS” if the number is positive, “NEG” if the number is negative, and “ZERO” if the number is zero. There’s also some test cases below so you can confirm that you’ve written your program correctly.
def num_eval(num): # Your code here >>> num_eval(-7) NEG >>> num_eval(126492) POS >>> num_eval(0) ZERO
Once you’ve gotten that function working, let’s try writing a function that returns a value. Write a function that takes a number to the power given in the arguments using a while loop. The result should be returned to the user. Once again, the function declaration and some tests are below.
def power(num, pow): # Your code here >>> x = power(4, 3) >>> print(x) 64 >>> x = power(2, 16) >>> print(x) 65536
For an extra challenge, try some error checking! If the pow argument is negative or not an integer, the function should print an error message and return nothing. The test cases for that functionality are below.
>>> x = power(12, -3) ERROR: power must be positive >>> x = power(4, 0.5) ERROR: power must be a whole number
Congratulations! You now know all of the basic functionality of Python! We will use all of these concepts as we move into the projects. Next week, we are going to start a new project and make some art!