Skip to content

Instantly share code, notes, and snippets.

@algotruneman
Created June 26, 2013 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save algotruneman/5868694 to your computer and use it in GitHub Desktop.
Save algotruneman/5868694 to your computer and use it in GitHub Desktop.
# 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