So far we have learned how to use built-in functions, import math, and random modules, use functions from these modules and define your own functions to show as output some strings using the print function. Now we are going to create functions that will return value from a function and assign it to a variable outside a function body. The general form of function with the return keyword can be written in the following form.
When the function is called it should be assigned to a variable. When the code has been executed the variable under which the function is called will contain the return value.
Example 1 Create a function that performs the addition of two numbers and returns the result.
Solution: The function will be defined using the def keyword followed by the function name (add) followed by the set of brackets in which we will put two arguments x, and y and finally followed by the colon (:). The arguments x and y do not have to be defined yet they will be defined later. In the function body, we will create a variable result that will be equal to x + y, and the value of the result variable will be returned using the return keyword.
Example 2 - Create four basic math functions (addition, subtraction, multiplication, division) and test them.
Solution: This example is practically the extension of the first example. In the previous example, we have created only the add (addition) function that adds two variables. Now three more functions will be created and these are subtraction, multiplication, and division. The key difference between this example and the previous one is that in this example the print function is inside the function body which means that the output will be shown each time the function is called. The second difference is that the return keyword will be followed with x+y or x-y or x*y or x/y, respectively. This means that the addition, subtraction, multiplication, or division will be performed in the line in which the return keyword is used. In Example 1 we have created variable name result to which the x+y was assigned and then used return result to return the result of addition. In this example, by creating return x+y we have reduced one line of code and unnecessary definition of the local variable result.
def FunctionName(argument1, argument2,...):The last command return result will return the value that was assigned in the function body to variable name result. The result variable does not exist outside the function body so it is a local variable. Using the return command the function will produce the output which can be of any type (integer, float, string, list, tuple, dictionary,...).
#Function body: The function does some stuff here
return result
When the function is called it should be assigned to a variable. When the code has been executed the variable under which the function is called will contain the return value.
varName = FunctionName(argument1, argument2,...)In this case the varName variable after code is executed will contain the value of local variable result which was defined inside the function FuncitonName.
Example 1 Create a function that performs the addition of two numbers and returns the result.
Solution: The function will be defined using the def keyword followed by the function name (add) followed by the set of brackets in which we will put two arguments x, and y and finally followed by the colon (:). The arguments x and y do not have to be defined yet they will be defined later. In the function body, we will create a variable result that will be equal to x + y, and the value of the result variable will be returned using the return keyword.
def add(x,y):To test the function add we will define two variables named x1 and y1 and assign values 5 and 6, respectively. Then we will call the add function with arguments x1 and y1. The function add will be assigned to variable name res. Finally, we will show the output in the form x1 + y1 = res.
result = x + y
return result
x1 = 5The entire code created in this example is given below.
y1 = 6
res = add(x1,y1)
print("{} + {} = {}".format(x1,y1,res))
def add(x,y):Output
result = x + y
return result
x1 = 5
y1 = 6
res = add(x,y)
print("{} + {} = {}".format(x1,y1,res))
5 + 6 = 11
Example 2 - Create four basic math functions (addition, subtraction, multiplication, division) and test them.
Solution: This example is practically the extension of the first example. In the previous example, we have created only the add (addition) function that adds two variables. Now three more functions will be created and these are subtraction, multiplication, and division. The key difference between this example and the previous one is that in this example the print function is inside the function body which means that the output will be shown each time the function is called. The second difference is that the return keyword will be followed with x+y or x-y or x*y or x/y, respectively. This means that the addition, subtraction, multiplication, or division will be performed in the line in which the return keyword is used. In Example 1 we have created variable name result to which the x+y was assigned and then used return result to return the result of addition. In this example, by creating return x+y we have reduced one line of code and unnecessary definition of the local variable result.
def add(x,y):As seen from the previous block of code four mathematical functions were created i.e. addition, subtraction, multiplication, and division. Each function has a print function inside that will print the x, y, and the result of the mathematical operation. The second and final line in each function body has a return keyword with mathematical operations applied on x and y. When the function is called and assigned to a variable for example addtest = add(x,y). This means that the function body will be executed i.e. the output will be shown in this case x+y = result. Besides that, the resulting value of x+y will be returned using the return keyword and assigned to a variable named addtest. So if we use the print function on the variable addtest it will show the value of the variable addtest. Output:
print("{} + {} = {}".format(x,y,x+y)
return x+y
div sub(x,y):
print("{} - {} = {}".format(x,y,x-y))
return x-y
def mul(x,y):
print("{}*{} = {}".format(x,y,x*y))
return x*y
def div(x,y):
print("{}/{}= {}".format(x,y,x/y))
return x/y
addtest = add(150,50)
subtest = sub(150,50)
multest = mul(150,50)
divtest = div(150,50)
150 + 50 = 200
150 - 50 = 100
150 * 50 = 7500
150/50 = 3.0