Please enable JavaScript to use CodeHS

Basic Functions in Python

By Ryan Hart

Functions are one of the most important topics to master when programming in Python. They help us organize our code, make it easier to understand, avoid repeating code, and make our program run more efficiently. That’s a lot of benefits! Up to this point, without functions, our program runs like a long morning to do list:

Morning List

Functions allow us to break the long list into conceptual blocks of tasks, where each block would be a specific function:

Grouped Activities

When we use these morning functions, our code is much easier to read and understand:
Breakfast Functions

And if your brother wakes up and wants toast and eggs as well, you can call those functions again with only 2 lines instead of the original 11:

Breakfast Functions

Functions in Python

The same logic above applies to using functions in Python. Functions complete a specific job that may require a series of tasks. When we want to run the function, all we have to do is define the function, specifying the desired tasks or commands, and then call the function to run through those tasks. We can even give the function inputs, called parameters, that provide extra information for the function to use – like the number of eggs desired for the Make Eggs function above.

Take a look at the following slides to see an example in Python:


As you can see from the last slide, using the triple_number() function makes our program much more efficient.
Function

Here are a few important points when making functions in your program:

  • Functions should only do one thing. That is, the triple_number() should not triple a number AND add 10. That would be confusing.
  • The function’s name follows the same naming convention as variables – lowercase, underscore between words, and identifies the job the function is doing.
  • Variables created inside a function, including parameter variables, can only be used inside the function.

Take a look at this example in the code editor below.

  • Try calling the function with the numbers 11 and 15. See how easy it is once the function has been defined?
  • What happens if you try using the parameter variable num outside of the function? Try writing print(num) outside of the function.
  • Try deleting the word def when you are defining the triple_number() function. What happens?

More function examples

This next example takes in two parameters in order to calculate the area and perimeter of a rectangle.

  • Add a couple more call statements with new numbers.
  • These functions require two parameters in order to run. See what happens when you only have one or zero in the call statements or function definitions.
  • Notice the parameters are defined as w for width and then l for length, in that order. What happens if you reverse the numbers in a call statement?


The code snippet below comes from a wedding invite program, in which the guest is asked if they prefer a vegetarian or meat meal. Based on the guest’s choice, the function food_item() then prints out the relevant meal.

  • What happens when you move the function definition (lines 1 - 7) after the user prompt and function call statement (line 12)? Do you need to define the function before or after you call it, or does it not matter?
  • Write a second user input that asks the guest if they prefer to stay in a room in a hotel or a tiny house, then create a function to print the name of the hotel or tiny house depending on their choice.

Now it’s your turn!

Use the editor below to try to build out some functions of your own. Remember, you need to both define the function and call it in order to see the results.

  • Write a function that takes in a person’s name as a parameter and prints out a greeting to that name.
  • Write two functions that take in the radius of a circle as a parameter. In one, calculate and print the area (A = 3.41 * radius ^ 2). In the other, calculate and print the circumference (C = 2 * 3.41 * radius) of the circle.
  • Write a function that takes in two numbers and multiplies them together, printing out the result.