Python for Beginners 08: Creating Functions

Knowing how functions work, we are now going to create one on our own. Maybe you remember that in a previous exercise, we converted temperatures from Fahrenheit into Celsius and rounded the result to two decimals.

Imagine you would need to do this many, many times so you would want to find a way to automate it. You could create a function.

There are 6 steps to create a function:

  1. Let Python know you are going to define a function by writing def.
  2. Give your function a name (to call it later on) and write it behind def (one blank space in between). In our example, I call my function temp_conversion. The rules for naming functions are the same as for variables: the first character can’t be a number; it cannot be a keyword (a word that is already defined in Python). A function should also not have the same name as a variable.
  3. Define the argument. Write two brackets (directly after the name) and put the names of the variable(s) you want to have as argument(s) inside. If you have more than one, you need to separate it by commas (and a blank space after it). You can also leave it blank. temp_conversion only has one argument, the temperature in degrees Fahrenheit represented by the variable F.
  4. Finish the so-called header (first line of a function) by writing a colon after the final bracket of the argument.
  5. The rest of the function, called the body, has to be indented. Then you can define the steps you want your function to implement. My new function temp_conversion should define the temperature in degrees Celsius using a variable C and the conversion formula (I just copied it from the conversion we did before). Afterwards, it should round it to two decimals (again, just copied from before).
  6. At the end, you need to define what your function is supposed to return by writing it inside the brackets of the return function. In the example, I just put C.

Now that we have defined the function, we can call it. Remember that we do this by just writing the name with values for the arguments in brackets. Here, I put 200 as the value for F.

Don’t worry if you still see no result in the console. As always, you need to print the function or a variable to which you have assigned the return value of your function in order to see any results.

One final piece of advice: The function only works as long as you do not create another function with the same name. As Python can only remember one function per name, it will just use the one most recently executed and forget the other one. You should be able to avoid this by giving meaningful names to your functions. However, what is useful to know is that Python does not consider the names to be identical if the functions have different numbers of arguments, i.e. you can have multiple functions with the same name as long as they have a different number of arguments.

Like some previous posts, this one was inspired by Think Python by Allen B. Downey. If you want a more complete approach to learning Python (including all the theory), you can have a look at the free pdf version.

#mastery08

Leave a comment