Functions in Python, explained in details for complete beginners

One who has mastered the basic Python utilities such as variables, numbers, lists, tuples and dictionaries; should start to learn about functions and their usage, unless they want to remain in the beginner stage and stop progressing with their journey as a Python coder.

Based on my personal experience with the Python programming language so far, a function is the tool which groups a set of code statements together with the main purpose of helping the coder to utilize the same resources more than one in their applications, without repeating themselves.

By using functions not only do Python coders reduce their future work, but they also group code instructions together with the main purpose of developing a specific utility which solves a specific problem. For example, the following function written in Python computer programming language, is specifically developed to find the sum of two numbers and return the result back to the user.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

def find_sum(a, b): return a + b

The above Python function takes as input two parameters,a and b. Based on the value of the parameters, it finds their sum and returns it to the user. As you may know, Python supports arithmetic operations by default.

a + b # python arithmetic operation

Why use functions in Python

There are many reasons why Python coders make use of function in their computer programming projects, but the following is the main two.

  • To maximize code reuse
  • To minimize code redundancy

Another obvious reason why those who code computer software make use of functions, is to split their project into small components which make the process of application development really easy and simple to track the code statements under the hood.

Since Python is an object oriented computer programming language, it is always a good idea to bring practical examples with the main purpose of making the theory completely easy to understand for everyone, even to those who have no coding experience at all.

Imagine if someone gave you a project which deals with the wrapping of the ffmpeg multimedia framework. Wouldn’t it make sense to write specific functions for extracting audio, cutting the videos, extracting images?

Based on my personal experience with Python computer programming language, it would make totally sense to split the project into simple components, by using specific functions. The following is a simple architecture I am writing to demonstrate how Python functions help in splitting the project into small utilities.

ffmpeg.extract_audio
ffmpeg.cut_video
ffmpeg.extract_images

In the above block of Python code, the ffmpeg is the module, and the others is the functions; the small components which build the functionalities of the project.

How to declare a function in Python

Python coders declare functions by using the def statement, which means to define a new function. The example shown below, is a practical one.

def new_function():pass

The pass statement is being used to skip the further coding of the function, it can be used in class statements too like shown below.

class A:pass

One can easily write a new function in Python by using the def statement. For a Python function to properly work, it must return something, a value. The Python return statement is being used to exit the function and send back the value to its caller.

def find_sum(a, b, c):return a + b + c

Once a function is being declared with the help of the def statement, a new object is created and assigned to the name of the function. According to the official Python documentation, the function does not exist until the moment the interpreter reaches and runs the def statement where the function is defined.

A Python function can be assigned to a variable, stored in a list and even nested in if statements or while conditional loops.

For example, the following example illustrates how a function can be nested inside a Python if statement.

if execute:find_sum(1, 2, 3)

Time to open the Python interpreter and put some theory about functions in practice. Once you have launched the Python shell, make sure to write the following function.

def display_name(name):pass

The above function does not do anything at the moment, since we just declared it and passed with the Python pass statement.

Try to extend the above function like shown below.

def display_name(name):print(name)

Our function is not ready to be put in work. Although a very simple one, it is good enough to materialize the theory and the concepts we have covered so far.

The Python function display_name takes an input and displays it on the screen, by making use of the builtin function called print.

Run the following and see what happens.

display_name(‘codetheory’)

One can easily assign a function to a variable, and do the call from the variable. A practical example to illustrate such theory is being shown below.

f = display_name
f(‘codetheory’)

The above function does not return anything, it just displays stuff on the console with the help of the builtin Python print statement.

A function can also have arbitrary user-defined attributes, with the main purpose of recording data which can be used later. Take the following example, a simple function which calculates the final price of a product based on the tax.

def calc_price_of_product(price):
tax = 30
final_price = tax + price
return final_price

How to call a function in Python

Once a function is declared, for it to work, it must be called. One can easily call a function in Python by using the following syntax.

my_function()

Notice the brackets in the above call. Based on the declaration of the function, one needs to pass the right number of arguments as an input, before calling it; otherwise they will run into errors thrown by the Python interpreter.

A call of the Python function display_name needs to be done with an argument as an input. Try to call the Python function display_name like shown below and see for yourself what happens in the interactive interpreter.

display_name()

The interpreter will throw the following error, telling the user that they need to run the function with exactly one argument.

Traceback (most recent call last):
File “”, line 1, in
TypeError: display_name() takes exactly 1 argument (0 given)

Final thoughts

Functions make it very easy for the Python developer to write their projects without repeating themselves. Another reason why Python functions make a great utility when it comes to software development, is the fact that they help to split a big project into small components, specific to carry out certain tasks.

Through this article, we went through basic concepts of Python functions, knowledge which everyone should master in order to progress on their journey as Python coders, especially the complete beginners.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

One thought on “Functions in Python, explained in details for complete beginners”

Leave a Reply

Your email address will not be published. Required fields are marked *