We can return the result or output from the function using a 'return' statement in the body of the function. For example,
return c #returns c value out of function
return 100 #returns 100
return lst #return the list that contains values
return x, y, c #returns 3 values
A function returns a single value in the programming languages like C or Java. But in Python, a function can return multiple values. When a function calculates multiple results and wants to return the results, we can use the return statement as:
return a, b, c
Here, three values which are in 'a', 'b' and 'c' are returned. These values are returned by the function as a tuple. Please remember a tuple is like a list that contains a group of elements. To grab these values, we can use three variables at the time of calling the function as:
x, y, z = function()
def sum_sub(a, b):
c = a + b
d = a - b
d = a - b
return c, d
A function that returns the results of addition, subtraction, multiplication and division.
A function that returns the results of (a+b)2, (a-b)2, (a+b)3, (a-b)3.