Created
June 26, 2013 16:00
-
-
Save algotruneman/5868694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Two function Samples | |
# First version doesn't export the calculation out of the function | |
# even though the function itself works just fine. | |
area = 0 # initialize the global variable | |
def area_rectangle(length,width): | |
area = length * width | |
print "inside function",area # area is evaluated inside the function 35 in my example | |
return area | |
area_rectangle(5,7) | |
# This function call succeeds in getting length and width into the function but not back out. | |
print "Outside the function (global) variable : ", area | |
# evaluates to zero not the expected 35. | |
#################################### | |
# This second version WORKS. | |
area = 0 # initialize the global variable | |
def area_rectangle(length,width): | |
area = length * width | |
print "inside function",area # area is evaluated inside the function | |
return area | |
area = area_rectangle(5,7) # Works because the returned "inside" local var is assigned to the "outside" global var. | |
print "Outside the function (global) variable : ", area | |
## This works because of the global var assignment to the result of the function call. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment