Function HELP

Hey guys!

I just started to learn coding to aid in my eventual goal of programing PLCs for CNC controls. I thought that getting my brain trained in Python would help gain some elementary knowledge in this field.

I am on the “Functions” lesson and am struggling. I just can’t seem to get it. Does anyone have some added resources I can use to gain further understanding? The order of operations seem to be tripping me up.

If you could provide any information it would be greatly appreciated!

Thank you,
Tim V

Hello @timvincent3364370773, welcome to the forums!

Do you mean the order of operations in Maths? If so, here is a good article explaining it.

Here is a good article on Python functions. In Python, functions are just code that is given a name so it can be repeated throughout the program. Think of it as a recording of you playing a piece of music. Say you wanted to show to your three groups of friends on three different days how well you could play Ode to Joy. Instead of physically playing it to each group, you could play it once and record it. This is essentially what a function is-a ‘record’ if you will, of code, which can be used throughout the program without you having to re-write it.

I hope this helps!

These resources do help, after clearing my head I revisited the lesson this morning. Now I have this question if you could clarify when you have a minute…

I can’t understand this to save my life…

def get_boundaries(target, margin):
a = target - margin
b = target + margin
return a, b

a, b = get_boundaries(100, 20)

print("Low limit: “+str(a)+”, high limit: "+str(b))

Output: Low limit: 80, high limit: 120

I understand that if I flip “a” and “b” within the function I will get this as my output…Low limit: 120, high limit: 80

But what I don’t understand is if I also flip “return a, b” to “return b, a” I also get…Low limit: 120, high limit: 80

The same goes for if I flip “a, b” when I call the function.

What the freak is going on?!?!

It is because a is saving the low limit value, were as b is saving the high limit value.
Because of this, if you reverse them at all than you reverse the values being output.

You are returning two values and saving them to two variables:

The first value returned will be assigned to the first variable, and the second value to the second variable.

Hello, @timvincent3364370773, and welcome to the Codecademy Forums!

This reference table is useful for looking up the order of operations: Operator precedence

Operations listed at the bottom have highest precedence, and are performed first, with those at the top performed last.

1 Like

So they are positional!? similar to function arguments?

Thanks!

1 Like

Hello! Yes, they are positional.

You are saving target + margin (or b in return b, a) to a in a, b = get_boundaries(100, 20).

You are also saving target - margin (or a in return a, b) to b in a, b = get_boundaries(100, 20).