Q. Can a function return multiple values how?


Answer =

Yes.

(a)

Either receive the returned values in form a tuple variable.

For example: -

def squared(x, y, z):
    return x, y *y, z * z

t = squared (2,3,4)
print(t)

Output is:-

 
Tuple t will be printed as:

(4, 9, 16)


(b)

Or you can directly unpack the received values of tuple by specifying the same number of variables on the left-hand side of the assignment in function call.

For example: -

def squared(x, y, z):
    return x* x, y * y, z * z

v1, v2, v3 = squared (2,3,4)
print("The returned values are as under:")
print (v1, v2, v3)

Output is:-

4 9 16

5 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Bro....In first code 2nd line, in return statement. Check it.

    ReplyDelete
  2. Actually, there's slight little mistake that's in 2nd line. In return it should be x*x, y*y,z*z but instead it's x,y*y,z*z.... As per the output.

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post