# Difference between low, high and low_limit, high_limit?

Why do we need to redefine something we’ve already defined?

We’ve defined low_limit and high_limit with equations and then when we tell the program get_boundaries(100, 20) this should tell what each key word in the equation means. Perhaps I take the ease with which computer do things normal for granted but having to introduce low and high seems to be an unnecessary step logically speaking.

The variables that are defined inside the function are local only to that function. They cannot be seen. We need to return their values to see those.

``````def foo(m, n):
return m - n, m + n

a, b = foo(100, 20)
``````

What we are doing is unpacking a sequence. The return value contains multiple values, so it is packaged as a sequence (a tuple, probably).

1 Like

I would say it’s more to keep the code clear and simple to understand as if you use the same variable from the function like

Blockquote
print(low_limit)#would give you error to define the variable since you did define it as low
print(high_limit)
Blockquote

more or less keeping thing clear and consistent to understand when you review code later.

When naming variables we try to include as much information as can given.

``````low

high
``````

Those are very general, with not a lot of interpretive information.

``````low_limit

high_limit
``````

Those are less general, and imply lower and upper bounds. More specific, and easier to rationalize for the reader.

2 Likes

I was a little confused at first by over-complicating something simple. You must define the new variables before they can be used:

``````def get_boundaries(target, margin):
low_limit = target - margin
high_limit = margin + target
return low_limit, high_limit
low = low_limit
high = high_limit
low, high = get_boundaries(100, 20)
``````

-lif

Hello, @tera7835798474.

Welcome to the forums!

I formatted your code, so we can see how you had originally indented it. Please review this post: How do I format code in my posts?
Regarding your code, these lines will never be executed:

Consider what `return` does. There is also no need for those lines to be executed. Values assigned to variable inside of a function have local scope (are only accessible inside the function). The values that are `return`ed by the function are ‘unpacked’ and assigned to `low` and `high` in this line:

``````low, high = get_boundaries(100, 20)
``````
1 Like

You are correct the variables inside a function are local to that function unless shenanigans are engaged, but that is outside the scope of this question.

If you’re curious there’s a good discussion of it over on stack overflow:

It has several commented code examples to help get the finer points across.

While a function can have multiple return statements. It can only return once.

So in this case it returns the first calculation, then exits.

This is why the example saves the calculations as variables and returns them both in a single return.

3 Likes

There are two schools of thought on the subject of `return`. That much is clear.

One school accepts multiple return branches, the other insists upon one, only. Can we find legitimate arguments for each scenario?

See if we can fill out this question with more information, enough evidence for us to draw our own conclusion. Be sure to link to any sources quoted.

This is a very good explanation, thank you for clearing this up!

I understand how it makes sense but it was very confusing as we have not set parameters in this way so far in the course.

I found it much more intuitive to remain consistent with the variable naming scheme. I also added a print() line to check my work which seemed to vindicate my approach. Let me know if this doesn’t make sense to anyone else as I am pretty new to this stuff.

Hello everyone. I was also confused as to why low_limit and high_limit were abbreviated with low and high when we printed the equation.

I decided to change the abbreviation to something else. Bob for low_limit and Mary for high_limit. I printed and got the same output of 120 and 80.

This was my syntax:

def get_boundaries(target, margin):
low_limit = target - margin
high_limit = margin + target
return low_limit, high_limit

bob, mary = get_boundaries(100, 20)
print(bob)
print(mary)

This made me conclude that the abbreviation (or variable of the variable) is defined by the position within the commas of:

bob (1st position), mary (second position) = get_boundaries(100, 20)

in comparison to the position of the variables low_limit, high_limit within the return line:

return low_limit (1st position), high_limit (2nd position)

Hope this helps!

2 Likes
``````def get_boundaries(target, margin):

low_limit = target - margin
high_limit = margin + target
return low_limit, high_limit
# Two vairables areassigned to the same function call thereby returning two different values/results.
low, high = get_boundaries(100, 20)
# A print statement of the above removes the confusion
print(low, high)
``````

Thanks, this helped me, I was clueless as to how I got my answer when the variable names do not match up, but it’s not about the variable names. You are calling the return values in order and the names do not have to match up, which confused me at first but seemed like the only explainable way for my programs results.

Why is my code wrong ?

def get_boundaries(target, margin):
low_limit = target - margin
high_limit = target + margin
return low_limit, high_limit
low, high = get_boundaries(100, 20)
return low, high

We cannot see how your blocks are indented, but the last line does not belong. The line above it should not be indented (not inside the block).

you’re meant to save the return value, like this
low = print(low_limit)
high = print(high_limit)

@mtf’s response about checking indentation and a moving a particular line outside the function block would be correct. In your code you’re using print as a function, which will print to the console, but it will return `None` to the names `low` and `high`.

Maybe I need more coffee, and I’m sure the answer is simple, but, I still have this question. Why save the values globally(?) when you can plug in any two numbers in the `print()` function?

``````def get_boundaries(target, margin):
low_limit = target - margin
high_limit = margin + target

return(low_limit, high_limit)

#low, high = get_boundaries(100,20)

print(get_boundaries(100, 20))
print(get_boundaries(200, 40))
print(get_boundaries(50, 95)
``````