Shankar Kumar
Shankar Kumar

Reputation: 2357

Python returning variables from various functions

from __future__ import division
import math

def main():
    the_discriminant = discrim(1,0,-4)
    print the_discriminant
    the_rest(discrim,b,a)

def discrim(a,b,c):
    discriminant = math.sqrt(math.pow(b,2)-4*a*c)
    return discriminant, b,a

def the_rest(discrim,b,a):
    x = ((-b + discriminant) / 2*a)
    y = ((-b - discriminant) / 2*a)
    print x,y

if __name__ == '__main__':
    main()

I am fairly new to Python, and I'm playing with writing functions and returning variables, I'm a little confused on how to correct the code. I am writing a quadratic solver program, but I need to use the discriminant and a,b,c values in "the rest" function. (which does the rest of the equation.) I'm kind of confused on how to return the values and use them in another function. Thanks!

Upvotes: 2

Views: 387

Answers (4)

Preet Kukreti
Preet Kukreti

Reputation: 8607

while jamylak's answer is correct, it can also be much more maintainable to return a simple class. Then if you ever change your function/return values/representation, the calling code:

  • is name/identifier-based; it is very flexible to change; its not order dependent, or tuple length dependent. It is also saves you typing and unnecessary duplication of ordering implied rules throughout your code.
  • if there IS a breaking change the interpreter will error on module load instead of at runtime, so you are not going to miss the error. This is because you are trying to access named members, and are not relying on some "hidden" or "implied" rule like tuple ordering that is not formalised anywhere in the code.

For a larger project this is definitely the way to go.

Upvotes: 1

Brian Cain
Brian Cain

Reputation: 14619

There's nothing wrong with returning tuples like in your version of discrim. But the code just doesn't make as much sense (IMO) that way.

Try it like so:

#!/usr/bin/env python

from __future__ import division
import math

def main():
    a = 1
    b = 0
    c = -4
    the_discriminant = discrim(a, b, c)
    print the_discriminant
    x, y = the_rest(the_discriminant,b,a)
    print x, y

def discrim(a,b,c):
    discriminant = math.sqrt(math.pow(b,2)-4*a*c)
    return discriminant

def the_rest(d, b,a):
    x = ((-b + d) / 2*a)
    y = ((-b - d) / 2*a)
    return x,y

if __name__ == '__main__':
    main()

Upvotes: 0

Ryan Haining
Ryan Haining

Reputation: 36802

I believe this is what you're trying to do. your discrim function returns a tuple (similar to an array). Then when you call the_rest using a * indicates that you want to send the elements of the tuple, rather than the tuple itself as one argument

from __future__ import division
import math

def main():
    the_discriminant = discrim(1,0,-4)
    print the_discriminant
    the_rest(*the_discriminant)

def discrim(a,b,c):
    discriminant = math.sqrt(math.pow(b,2)-4*a*c)
    return discriminant, b,a

def the_rest(discrim,b,a):
    x = (-b + discrim) / (2*a)
    y = (-b - discrim) / (2*a)
    return x, y

if __name__ == '__main__':
    main()

Upvotes: 2

jamylak
jamylak

Reputation: 133554

the_rest(*the_discriminant)

or (and I prefer this method):

d, b, a = discrim(1, 0, -4)
the_rest(d, b, a)

Upvotes: 7

Related Questions