KnowledgeBoat Logo

Computer Science

Find and write the output of the following python code :

def Call(P = 40, Q = 20):
    P = P + Q
    Q = P - Q 
    print(P, '@', Q)
    return P
R = 200
S = 100
R = Call(R, S)
print(R, '@', S) 
S = Call(S)
print(R, '@', S)

Python

Python Functions

44 Likes

Answer

300 @ 200
300 @ 100
120 @ 100
300 @ 120

Working

  1. Function Call is defined with two parameters P and Q with default values 40 and 20 respectively.
  2. Inside the function Call, P is reassigned to the sum of its original value P and the value of Q.
  3. Q is reassigned to the difference between the new value of P and the original value of Q.
  4. Prints the current values of P and Q separated by @.
  5. The function returns the final value of P.
  6. Two variables R and S are initialized with values 200 and 100 respectively.
  7. The function Call is called with arguments R and S, which are 200 and 100 respectively. Inside the function, P becomes 200 + 100 = 300 and Q becomes 300 - 100 = 200. So, 300 and 200 are printed. The function returns 300, which is then assigned to R. Therefore, R becomes 300.
  8. S = Call(S) — The function Call is called with only one argument S, which is 100. Since the default value of Q is 20, P becomes 100 + 20 = 120, and Q becomes 120 - 20 = 100. So, 120 and 100 are printed. The function returns 120, which is then assigned to S. Therefore, S becomes 120.

Answered By

13 Likes


Related Questions