-2

I am writing a function to calculate the distance of an object using a HCSR04 sensor. The sensor sends out a sound and waits for the return. Using the time it takes for the sound to return, I can calculate the distance of the object. My function performs this task and returns the distance in centimeters. I use the returned value to judge if my robot can continue moving forwards and in which direction to turn when an object is blocking its path.

I use the function as follows:

def sides():
    if sen.dist_1() + sen.dist_5() < stg.safe_front_dist:
        print("Sides Checked - Going back.")
        return "back"

    elif sen.dist_1() > sen.dist_5():
        print("Sides Checked - Left.")
        return "left"

    elif sen.dist_1() < sen.dist_5():
        print("Sides Checked - Right.")
        return "right"

    else:
        print("Error checking sides.")
        return "error"

While this works without problems, I am wondering if it is better to assign the values of each function to a variable then compare the variables. This would look like:

def sides():
    dist_1 = sen.dist_1()
    dist_5 = sen.dist_5()

    if dist_1 + dist_5 < stg.safe_front_dist:
        print("Sides Checked - Going back.")
        return "back"

    elif dist_1 > dist_5:
        print("Sides Checked - Left.")
        return "left"

    elif dist_1 < dist_5:
        print("Sides Checked - Right.")
        return "right"

    else:
        print("Error checking sides.")
        return "error"
Kattie.S
  • 117
  • 1
  • 11
Psuedohim
  • 1
  • 1
  • 1
    In *this specific case*, there are significant efficiency gains to only calling your function once. In *the general case*, this is a question that doesn't have a canonical answer, and is generally a matter of opinion -- which we try to stay out of on this site. – Charles Duffy Jun 08 '18 at 21:02

1 Answers1

0

I am wondering if it is better to assign the values of each function to a variable then compare the variables.

It depends on the case, really. In your case, since you're calling the function many times, you could save resources by only calling the function once and assigning the return value to a variable. That way, you avoid having to repeatedly call the function and recalculate it's return value.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • That does make sense. So if I were to only call the functions once then assigning the returned value to a variable would not be any more effecient. Correct? – Psuedohim Jun 08 '18 at 21:06
  • Exactly. Calling a function multiple times wastes resources. Call the function, once, and the store it's value for later use. – Christian Dean Jun 08 '18 at 21:09
  • 1
    ... although it does depend on the function. This can be done if the function is *idempotent*, that is, it has no side effects when called and always gives the same result for a given input. See https://stackoverflow.com/questions/1077412/what-is-an-idempotent-operation?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – cdarke Jun 08 '18 at 21:19