Question

I am learning Jython using a program called JES, and I am having trouble understanding how to return values from a function that can be used in another function.

For example:

def driver():
  picture = makePicture(pickAFile())
  input(picture)
  changeRed(picture, reduceRedAmount)

def input(picture):
  redReduceAmount = requestInteger("How much should I reduce red by?")
  return redReduceAmount
def changeRed(picture, reduceRedAmount):
  for p in getPixels(picture):
    value=getRed(p)
    setRed(p,value*0.9)

I want to be able to just run the driver function in the command area, and have that function include the other functions. I understand that results in functions are local and don't pass through, but I though that if you use 'return' then it stores that value for later use. Am I on the wrong track here? Surely there is a way to pass information between functions?

No correct solution

OTHER TIPS

Try this:
Any "-" just disregard them that is not part of the code, they are just there for spacing to help so that you don't get any syntax errors.

def driver():
   picture = makePicture(pickAFile())
   input(picture)
   changeRed(picture, reduceRedAmount)
   repaint(picture)`

or

def changeRed(picture, reduceRedAmount):
   for p in getPixels(picture):
      value=getRed(p)
      setRed(p,value*0.9)
      repaint(picture)

or

def changeRed(picture, reduceRedAmount):
   for p in getPixels(picture):
      value=getRed(p)
      setRed(p,value*0.9)
      show(picture)

or

def changeRed(picture, reduceRedAmount):
   for p in getPixels(picture):
      value=getRed(p)
      setRed(p,value*0.9)
   return(picture)`

One of those repaint, return, show should work to display the new image.

To display other values in command area just use "print something here" for example print 1+1 or print value.

the way I do it is declare it as a global variable the line after the def functionName() and at the LAST line of the functionName() code before you reduce the indent, type return variableNameYouWant.

check this:

def functionName() global variableNameYouWant .. .. return variableNameYouWant

What we are doing by following this is declaring the function you want to be a global variable (which must be done before it is delcared with a value, so best to do at the start of the def.) and then before you close the def by decreasing indentation, return the variable outside of the def.

I know it's an old question but I'm striving for upvotes as a newbie. :)

Edit: Thanks for the downvote, anon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top