Q:

Write a python program to create function calculation() such that it can accept two variables and calculate addition and subtraction.

belongs to collection: Python Functions Exercises

0

Return multiple values from a function

Write a program to create function calculation() such that it can accept two variables and calculate addition and subtraction. Also, it must return both addition and subtraction in a single return call.

Given:

def calculation(a, b):
    # Your Code

res = calculation(40, 10)
print(res)

Expected Output

50, 30

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Separate return values with a comma.

Solution:

In Python, to return multiple values from a function, use a comma to separate them.

Solution1:

def calculation(a, b):
    addition = a + b
    subtraction = a - b
    # return multiple values separated by comma
    return addition, subtraction

# get result in tuple format
res = calculation(40, 10)
print(res)

Solution2:

def calculation(a, b):
    return a + b, a - b

# get result in tuple format
# unpack tuple
add, sub = calculation(40, 10)
print(add, sub)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Create a function with default argument using pyth... >>
<< Write a python program to create function func1() ...