Writing your own custom functions in Python

Basic use cases for using functions in Python

The Future Of Work
Jun 4 · 3 min read
Image for post
Image for post

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 + b
add(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 * x
my_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 created
fah_cel(212) #Answer is 100.0
fah_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 result
cel_fah(32) #answer is 89.6
cel_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.

Python In Plain English

Go deeper with the language powering everything.

The Future Of Work

Written by

"The best way to create the future is to create it." This project seeks to prepare professionals for the future through the Application of Data Science and AI.

Python In Plain English

Go deeper with the language powering everything.

The Future Of Work

Written by

"The best way to create the future is to create it." This project seeks to prepare professionals for the future through the Application of Data Science and AI.

Python In Plain English

Go deeper with the language powering everything.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store