Functions in Python (27/100 Days of Python)

Martin Mirakyan
3 min readJan 28, 2023

--

Day 27 of the “100 Days of Python” blog post series covering functions

Python functions are a powerful and essential tool for software development. They allow developers to organize and reuse their code, making it more readable and maintainable. In this tutorial, we will discuss the different types of functions in Python and look at some examples of how they can be used in software applications.

Functions allow you to group a set of statements together to perform a specific task, and can be reused throughout your code. We have used functions like min, max, or print to obtain the minimum, or maximum of several numbers, or to print some values. We can also define our custom functions to eliminate repetitive code.

Creating a Function in Python

In Python, you create a function using the def keyword, followed by the name of the function and a set of parentheses. The statements that make up the function's body are indented underneath the function definition. Here is an example of a simple function that takes no arguments and prints "Hello, World!":

def greet():
print('Hello, World!')

# You can call this function by simply typing its name followed by parentheses:
greet() # This will print `Hello, World!`

A real-world example might be a mobile game that has a function called play_tutorial that runs a tutorial for new players when they start the game for the first time:

def play_tutorial():
print('Welcome to the tutorial.')
print('Learn how to play the game.')
print('Enjoy the game.')

play_tutorial()

Passing Arguments to Functions

Functions can also take arguments, which allow you to pass data into the function for it to use. You specify the arguments in the parentheses after the function name. Functions like min or max take different values as arguments to then calculate the minimum or the maximum out of those. We can define a custom function that accepts arguments to then operate with those. When implementing a social media application, one could have a function called post_to_timeline that takes a message and posts it to the user’s timeline.

def post_to_timeline(user_id, message):
print(f'Posting {message} on {user_id}\'s timeline')

post_to_timeline('anna123', 'Hey there')
# Posting Hey there on anna123's timeline

Return Values from Functions

In addition to performing a task, functions can also return a value. This allows you to use the result of the function in other parts of your code. The return statement is used to specify the value that the function should return. A function can perform a calculation, and then return the result of the calculation back so that result can be stored as a variable and used later on. Let’s say you’re developing an e-commerce application that could have a function called calculate_discount that takes the original price and a discount percentage as arguments and returns the discounted price:

def calculate_discount(price, discount_percentage):
return price - (price * discount_percentage / 100)

discounted_price = calculate_discount(100, 10)
print(discounted_price) # 90.0

Return Multiple Values from a Function

In Python, it’s also possible for a function to return multiple values. This is done by separating the values with a comma when returning them. For example, a function called calculate_cost_and_tax could take the price of a product as an argument and return both the cost and the tax of that product:

def calculate_cost_and_tax(price):
tax = price * 0.08
return price, tax

cost, tax = calculate_cost_and_tax(100)
print('Cost: ', cost) # Cost: 100
print('Tax: ', tax) # Tax: 8.0

This technique is very useful when you want to return multiple related values, for example, coordinates of a point (x,y) or RGB value of a color (red, green, blue). Another example could be a function that takes a date as an input and returns the day, month, and year separately.

def date_to_day_month_year(date):
day, month, year = date.split('-')
return day, month, year

day, month, year = date_to_day_month_year('28-01-2023')
print('Day: ', day) # Day: 28
print('Month: ', month) # Month: 01
print('Year: ', year) # Year: 2023

Returning multiple values in this way makes it easy to use the returned values in a structured way and can help to improve the readability and maintainability of your code.

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

Responses (1)