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].
Write a function that will multiply a given number by itself followed by adding 2
HInt: x^2 + 2 or x*x + 2