Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

Python for Basic Data Analysis: 2.3 Functions with Return Values

Get started on your learning journey towards data science using Python. Equip yourself with practical skills in Python programming for the purpose of basic data manipulation and analysis.

Functions with return values

Using return, you can write a function that returns a value. You can assign the returned values to a variable which can be used in whatever manner you choose.

This is useful when you need to use the results your function produces for something else.

First let's create a simple multiplication function. We will see an output of 40.


Now let us add 2 to the variable mul

print(mul+2)


With the updated function below, we will see an output of 42.


You can also create a function that takes in multiple arguments, and add them both together.

Results:
4
11
15

 

Since we do not declare variables in Python, this means that we can use the addition function we created above to add any 2 sets of data together as long as they are both the same type (string, float, integer).

 

Let's add these codes into our trinket and see what happens!

print(addition("one","time"))

print(addition([1,1],[1,1]))




The results shows that the function concatenated "one" and "time", and merged the 2 lists to become [1, 1, 1, 1].

Activity

Write a function that will multiply a given number by itself followed by adding 2
HInt: x^2 + 2 or x*x + 2