Suppose i have function like this.
JavaScript
x
7
1
def funA(a,b):
2
if a>5:
3
c=a+b
4
if a<5:
5
c=a-b
6
return c
7
So is it possible for me to use the variable C outside in another function like this? Is this a correct way?
JavaScript
1
5
1
def fun(foo)
2
if foo>5:
3
foo +=c
4
return foo
5
Advertisement
Answer
just use the returned value as a parameter
heres an example:
JavaScript
1
12
12
1
def funcA(x):
2
return x
3
4
def funcB(y):
5
return y
6
7
#pass to function
8
funcB(funcA(1))
9
10
#store to variables
11
z = funcA(1)
12
heres how it could apply to your case:
first off you’re only going to get one value from this since a cant be less than and greater than 5 at the same time, so im going to modify the function to give you both values as a tuple:
JavaScript
1
8
1
def funA(a,b):
2
return (a+b,a-b)
3
4
def funB(c,d):
5
print(c,d)
6
7
funB(funA(1,2)[0], funA(1,2)[1])
8
If you look closely, funA
returns a tuple of those values so you can access each one using their index
; first index is 0, and second index is 1 hence: funA(1,2)[0]
and funA(1,2)[1]