© 2018 - Analytics Link

A simple user defined function in Python

August 2, 2017

For our post on user-defined functions in R - click here! 

A function is essentially a block of re-usable code that takes inputs, performs a specific task, and subsequently provides an output when called.

 

You'll have seen these in action already, as Python contains a number of built in functions, for example print(), sum(), and max()

                                                              

However, in the case where we need to undertake a task specific to our needs, we can create a 'user-defined' function


Writing a Python function can be broken down into the following seven parts:

 

1. def

 

def is short for define, it is a Python keyword that signals we're creating a function

 

2. Function Name

 

This is what the function will be referred to as when we call the function later on

 

3. Input Variables

 

The names of the variable inputs that will be processed by the function when we call it later on.  These will be included within parenthesis, and if there are multiple inputs these need to be separated by a comma

 

4. :

 

In Python, a colon signals indented code - which is a requirement for function syntax

 

5. Function Body

 

The code specific to the task or calculation we want the function to undertake

 

6. return

 

Return is another Python keyword, as in this case it specifies what part of the task or calculation we want to output

 

7. Calling the Function

 

We use the function name to call our specific function, and provide the inputs for the function to process.  Running this will give us an output!

 

 

Example:  In this scenario we need a function that can take a Celsius temperature and convert it to Fahrenheit for us.

 

Here is the code:

 

def temp_converter(celsius):
    fahrenheit = celsius*9/5 + 32
    return(fahrenheit)

 

We start with the def keyword and follow this with our function name (temp_coverter). Following this, in parenthesis, we put the name of input variable (celsius) and end the line of code with a colon (:)


On the next line (remember to indent) we code in the calculation for Celsius to Fahrenheit conversion, and beneath this we specify that we want to return the value of Fahrenheit.

 

That's it - the function is complete!  

 

Let's now call this function and see it in action.

 

First, specify the function name (temp converter) and then provide an input value for Celsius.  The function will take this value and convert to Fahrenheit for us:

 

temp_coverter(0)
Out[3]: 32.0

 

temp_coverter(100)
Out[3]: 212.0

 

temp_coverter(35.5)
Out[3]: 95.6

 

It works!

 

Fun fact, there is one temperature where Celsius and Fahrenheit are the same...

 

temp_coverter(-40)
Out[3]: -40


Hope you enjoyed that run through of a simple user-defined function in Python - let us know how you get on!

 

If you found this helpful, please share!

 

For our post on user-defined functions in R - click here!

 

 

 

Share on Facebook
Please reload

Please reload

Recent Posts