Reputation: 1509
I feel like I am misunderstanding something really fundamental. I wrote a program to do some math:
import math
def func1(a, b, c):
print (f"We will now square {a}, {b}, {c} and add them")
y = (a**2) + (b**2) + (c**2)
print (f"y = ", y)
x = y/3
print ("x = ", x)
z = math.sqrt(x)
print ("z = ", z)
return z
func1(1, 5, 1)
This works, I get the desired output, which is Z. But in my mind, I should be able to write a function, return a value, then use that value in another function. I'd like to break up the steps in the above script to see them more clearly. I was unable to do this, but succeeded in putting all the math in one step, seen above. Ideally I'd write something and it would look like this...
Func1(a, b, c) Square numbers a,b,c. Add them then return value
Func2 Take value I returned from Func1, divide by 3. Return value.
Func3 Get square root of value from Func2
Thanks for any help.
Upvotes: 0
Views: 76
Reputation: 135
This might help ::
import math
def func1(a, b, c):
print (f"We will now square {a}, {b}, {c} and add them")
y = (a**2) + (b**2) + (c**2)
return y
def func2(y):
x = y/3
return x
def func3(x):
z = math.sqrt(x)
return z
p=func1(1, 5, 1)
q=func2(p)
r=func3(q)
print(r)
Upvotes: 0
Reputation: 654
Save the returned value of each function into a variable and pass it to the next function. For example:
import math
def func1(a, b, c):
return (a**2) + (b**2) + (c**2)
def func2(value):
return value/3
def func3(value):
return math.sqrt(value)
result = func1(1, 5, 1)
print(result)
result = func2(result)
print(result)
result = func3(result)
print(result)
Upvotes: 0
Reputation: 584
Yes you can do this just pass them in different functions as required. eg.
def func1(x,y,z):
.....
.....
return ans1
def func2 (ans1):
.....
.....
return ans2
ans1 = func1(1,2,3)
ans2 = func2(ans1)
and so on
Upvotes: 3