Q:

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

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now