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)
Answer
300 @ 200
300 @ 100
120 @ 100
300 @ 120
Working
- Function Call is defined with two parameters
P
andQ
with default values 40 and 20 respectively. - Inside the function Call,
P
is reassigned to the sum of its original valueP
and the value ofQ
. Q
is reassigned to the difference between the new value ofP
and the original value ofQ
.- Prints the current values of
P
andQ
separated by @. - The function returns the final value of
P
. - Two variables
R
andS
are initialized with values 200 and 100 respectively. - The function Call is called with arguments
R
andS
, which are 200 and 100 respectively. Inside the function,P
becomes 200 + 100 = 300 andQ
becomes 300 - 100 = 200. So, 300 and 200 are printed. The function returns 300, which is then assigned toR
. Therefore,R
becomes 300. S = Call(S)
— The function Call is called with only one argumentS
, which is 100. Since the default value ofQ
is 20,P
becomes 100 + 20 = 120, andQ
becomes 120 - 20 = 100. So, 120 and 100 are printed. The function returns 120, which is then assigned toS
. Therefore,S
becomes 120.
Related Questions
What are the errors in following codes ? Correct the code and predict output :
total = 0; def sum(arg1, arg2): total = arg1 + arg2; print("Total :", total) return total; sum(10, 20); print("Total :", total)
What are the errors in following codes ? Correct the code and predict output :
def Tot(Number) #Method to find Total Sum = 0 for C in Range (1, Number + 1): Sum += C RETURN Sum print (Tot[3]) #Function Calls print (Tot[6])
Consider the following code and write the flow of execution for this. Line numbers have been given for your reference.
1. def power(b, p): 2. y = b ** p 3. return y 4. 5. def calcSquare(x): 6. a = power(x, 2) 7. return a 8. 9. n = 5 10. result = calcSquare(n) 11. print(result)
What will the following function return ?
def addEm(x, y, z): print(x + y + z)