Using the output of a function as an input of another function

Tags: , ,



Suppose i have function like this.

def funA(a,b):
 if a>5:
   c=a+b
 if a<5:
   c=a-b
 return c

So is it possible for me to use the variable C outside in another function like this? Is this a correct way?

def fun(foo)
 if foo>5:
   foo +=c
 return foo

Answer

just use the returned value as a parameter

heres an example:

def funcA(x):
    return x

def funcB(y):
    return y

#pass to function
funcB(funcA(1))

#store to variables
z = funcA(1)

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:

def funA(a,b):
  return (a+b,a-b)

def funB(c,d):
    print(c,d)

funB(funA(1,2)[0], funA(1,2)[1])

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]



Source: stackoverflow