Learning Python From Scratch: Functions

Hello everyone, I’m Elif! I am one of the Sisterslab’s Women in Tech Academy project participants, supported by Toplum Gönüllüleri Vakfı. The project aims to empower 25 women between the ages of 20–28 with software training for 3 months and seek their participation in the workforce in the sector. You can find more information about the project at this link: https://sisterslab.co/women-in-tech-academy/

In this series of medium articles, I will try to cover python from scratch. And today we will learn about functions.

What are functions and why do we need to use them?

The function consists of a set of reusable operations. Thus, it allows us to write operations that we will need to use more than once, and then use them as we want. We don’t have to write the same code over and over. And with this way, we will get rid of redundant code pieces.

Function Types

There are two basic different types of functions in python, one of them is Built-in functions which are the functions defined before by the python programmers. For example, Python has a math library that has a lot of built-in math functions. By importing this library to our code, we can use many math functions without having to write them ourselves.

And the second type of function is the user-defined function. This way we can define a function that suits our needs and use it where we want.

Defining Functions

I have mentioned the ease of coding in Python in previous articles. Defining a function is easy with python.

We can define a function with the def keyword in python. To define all you need to do is write def your_new_function( if_there_is_parameter): after this line, you can write your function in your function body. You can see a basic function definition example in the picture below. The function gets one parameter and its string name variable. After defining it we printed the name using the format.

Defining Function in Python

In the example, we put a parameter in the function but this part is optional. We can also define functions without parameters. So how can we know if we need to define it with parameters or not? It's pretty easy actually, if you are going to use the function for calculations or you want to use it with different variables you should use a parameter. For example below, let's say you just want to write a function that only prints some string, if so you don’t need to use any parameters at all!

Defining function without parameters

After defining the function we can start writing our code in the function body. The function body is the part that contains the set of operations. All the examples we did before were about printing things. So what we should do if we want to return some value at the end of the function? To return something from the function you need to use the return keyword with a value. If you used the return statement at the end of the function, after executing it you can use the return value later in the code. Like the example below we can add two numbers in the function body and then return the sum of them. In the line of result = add(3,9) we assigned the result value with the return value of the add function with parameters 3 and 9.

Adding two numbers together

Another important thing to know is the scope of the function. The values that we defined inside the function, are only accessible inside of it.

Global and Local Variables Used in the Function Scope

Let’s define a basic calculator function. In this function, we are getting two parameters and then we are calculating the basic arithmetic operations. After calculations, we are returning all the values and print them.

Basic Calculator

Using Default Parameters

When defining functions we can also want to use them with default parameters to not have any errors later on. Just like defining a function with a parameter, using a syntax like def my_function( default_value = “This is the default string”): is enough.

Using Default Parameters

Using the Same Function with Different Size of Parameters

Sometimes we do not know how many parameters we will use a function with, or we may want to write the same function with more than one parameter. In such cases, thanks to python, if we write the parameter we wrote in the function as a *parameter, we will indicate that the number of parameters we want will come into it. Thus, we do not need to write the same function with a different number of parameters. In the code snippet below we can see an example, even if we don’t send any parameters to the function, the function runs and returns a value. Or it works fine even if we send two or more parameters in it.

Defining the same function with different parameters

Today I tried to explain functions in python. I hope you liked this article. And I will continue to explain the python, with this series in the future. Have a nice day!

If you liked this article and want to see more don’t forget to follow me and my other social media accounts.

You can find all the resources I used in my article here!

Sources:

W3 Schools, Python Functions

Educative Python From Scratch

--

--

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
Elif Çelik

Elif Çelik

A Computer Engineer who is interested in Machine Learning and Deep Learning.