[Tutor] a question about passing values between functions

Alan Gauld alan.gauld at btinternet.com
Fri Nov 17 23:57:12 CET 2006


"kristinn didriksson" <kdidriksson at yahoo.com> wrote

> In my understanding, return area in the first routine
> makes the value of area an instance of areaCirc

No, it simply assigns the value returned by areaCirc to
area within XXXX.
areaCirc assigns

4*(math.pi)*(diameter/2)**2

to its local variable area then returns area.
So in effect the assignment line inside XXX
could simply be rewritten:

area = 4*(math.pi)*(diameter/2)**2

> Is that the right way to think of this program? Did I
> use return area correctly?

You used return area correctly but are getting confused
between objects and instances and functions.

functions (as defined with a def keyword) return values,
which may be instances of classes or builtin objects
Classes return instances of themselves.
But the syntax in both cases is thesame which is,
admittedly, confusing.

> Basically, I am wondering if I needed to use the
> return area statement.

Yes, that is how to pass data out of one function to
another part of the program.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

> ---------------
>        # 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()
> ---------------------------
>
>
>
> ____________________________________________________________________________________
> Sponsored Link
>
> Compare mortgage rates for today.
> Get up to 5 free quotes.
> Www2.nextag.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list