[Tutor] a question about passing values between functions

Bob Gailer bgailer at alum.rpi.edu
Fri Nov 17 21:14:28 CET 2006


kristinn didriksson wrote:
> Hello,
> I am still wrestling with the concept of values going
> between functions. (just starting out)The program
> below works seems to work, but here is my question.
> In my understanding, return area in the first routine
> makes the value of area an instance of areaCirc 
area = 4*( ... is what makes area a local variable (not instance). 
Assignment to a name in a function where the name has not been declared 
global makes that name local to the function.
> use areaCirc in the other program to call the value
> for the area that was calculated in the first routine.
> Is that the right way to think of this program? Did I
> use return area correctly?
>   
Yes.
> This is a new vocabulary, so I hope I am making sense.
> Basically, I am wondering if I needed to use the
> return area statement.
> Thanks,
> Kristinn
>
> ---------------
>         # this program is a redo of ex2 ch3 with a
> twist!
>       2 # use two functions--one to compute the area
> of a pizza, and one to
>       3 # to compute cost per square inch.
>       4 # Given are the diameter and the price. A =
> pi(r**2)
>       5
>       6 # define the function that computes the area
>       7 import math
>       8 def areaCirc():
>       9     diameter = input("Please input the
> diameter of the pizza: ")
>      10     area = 4*(math.pi)*(diameter/2)**2
>      11     print "The area of the pizza is %0.2f" %
> (area)
>      12     return area
>      13    
>      14
>      15 def unitCost():
>      16     price = input("Please input the cost of
> the pizza per square inch: ")
>      17     area = areaCirc()
>      18     cost = area * price
>      19     print "The cost of the pizza is %0.2f"
> %(cost)
>      20
>      21 unitCost()
> ---------------------------
If I were working this assignment I'd structure it thus:
get the diameter
get the cost per square inch
call a function to compute the area
call a function to compute the cost

-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list