0

Can someone please explain this code?

def f(x): return g(x)+3%2 def g(x): return x**2+2 print (f(g(1)))

10th Aug 2018, 1:36 PM
subia
3 Answers
+ 10
- The function g() takes a parameter x and returns the value x**2+2 ( For example, if x is 1..1**2+2 = 3). Thus, g(1) = 3 - Now, f(g(1)) becomes f(3). - The function f() also takes a parameter x ( here , x = 3) and returns the value of g(x) + 3%2 g(x) = g(3) = 11. g(x) + 3%2 now becomes => g(3) + 3%2 = 11 + 3%2 = 11 + 1 = 12 Therefore, it prints 12.
10th Aug 2018, 2:16 PM
Nikhil
Nikhil - avatar
+ 1
You have two defined functions here and a main body of your programm. The body states: print(f(g(1))) - it means it calls defined function g() with argument 1 and gets 1**2+2 = 3 in return. Then this result is passed to another function (f()): return g(3) + 3%2. Here function g() isa called again with argument 3 and returns 3**2+2 = 11. so now we have a simple equation: 11+3%2 = 11 + 1 = 12
10th Aug 2018, 2:17 PM
strawdog
strawdog - avatar
+ 1
Thank you so much for clearing my doubt.
10th Aug 2018, 2:38 PM
subia