How to call this function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
18th Jun 2022, 10:58 AM
Norberto Costa
Norberto Costa - avatar
9 Answers
+ 4
I think you just want to output the sum of two variables. You do not need to define h inside g for this. Just try this. g(x,y): return x+y #to see an output type the below line of code print(g(2,3))
18th Jun 2022, 12:06 PM
R_A
+ 3
Then you must know that a function can only be called inside its' scope. So your code should be modified like this: def g(x): def h(y): return x+y return h(5) #the "y" print(g(6))
18th Jun 2022, 12:40 PM
R_A
+ 3
Cause there's no such syntax in Python...
18th Jun 2022, 12:47 PM
R_A
+ 3
# You could do something like this and call funny()() def funny(a): return lambda b, a=a: a+b print(funny(3)(4))
18th Jun 2022, 7:44 PM
Lisa
Lisa - avatar
+ 3
Norberto Costa def g(x,y): def h(y): return y*2 return x + h(y) print(g(6,7)) #20 Access to h(y) is gained through g(y). g() then calls the result of h() and creates a new result when g() is called
18th Jun 2022, 8:14 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Norberto Costa I don't think you can call the function h() or assign h() a value because g() does not allow access to it
18th Jun 2022, 11:13 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
So how to modify it?
18th Jun 2022, 11:15 AM
Norberto Costa
Norberto Costa - avatar
0
No. I want to work with nested functions.
18th Jun 2022, 12:34 PM
Norberto Costa
Norberto Costa - avatar
0
Why can't call g(6)(5)
18th Jun 2022, 12:45 PM
Norberto Costa
Norberto Costa - avatar