Writing your own custom functions in Python

Basic use cases for using functions in Python

Functions are a great way for you to make recurrent processes easiser in scripting languages. They make it easy for you to divide codes into useful blocks, readable and time saving. Understanding the basic aesthetics of functions will be a great building block to help you understand how much more complex functions work.

This brief article has been carefully put together, to help you understand how functions work using very basic procedures. By the end of this article, you will understand why you need functions and how to write basic functions.

You can pass data known as parameters into a function and the function can return data as a result. To define/build a function, you use the keyword def:

`def my_function(): #my_function is the function name  print("My first function")`

The function above when run, simply gives the output statement which is My First Function. Let’s build a simple function, that accepts two numbers and returns the addition of such numbers.

`def add(a, b): #add is the function name, a and b are placeholders.    return a + badd(1,2) #The result is three#oradd(a = 1, b = 4)`

The basic function accepts two values, corresponding to the variables `a` and `b` which are placeholders as predefined in the function. We will define a simple function that takes in a value, multiplies the value by 5 and returns a result.

`def my_function(x):  return 5 * xmy_function(5) #The answer is 25`

Now, let’s do something a bit more advanced. We will define a function that converts temperature in degree Celsius to Fahrenheit.

The actual formula is (F − 32) × 5/9 = 0°C. So the function will have an impute value corresponding to the Fahrenheit scale to be converted to Celsius:

`def fah_cel(Fahrenheit): #Define function name and input var name    Fahrenheit = float(Fahrenheit) #convert input to float data type    celsius = (Fahrenheit - 32) * (5/9) #Create the function logic    return celsius #return the value of celsius variable createdfah_cel(212) #Answer is 100.0fah_cel(32) #Answer is 0.0`

We can go the other way round and build a Celsius to Fahrenheit converter. Using the function (C × 9/5) + 32. Applying the same logic as earlier used for the Fahrenheit to Celsius converter:

`def cel_fah(Celsius): #Define function name and input var name    Celsius = float(Celsius) #Convert the input value to float dtype    Fahrenheit = (Celsius * (9/5)) + 32 #Define the conversion logic    return Fahrenheit #return the resultcel_fah(32) #answer is 89.6cel_fah(100) #result is 212.0`

Functions can be very simple and become more complex, due to various nesting properties. This is a beginners guide to understanding how functions work in Python. Why don’t you take out time to play around with the little exercise. This will be a multiple part series, as we visit even more complex functions. This is a multiple series article, aimed to graduate you from basic functions to advanced concepts.

Future of Work Africa is a data analytics, consulting, and training company. We provide insights drawn from data to societies, organizations, and individuals make better decisions.

Written by