Inconsistency


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/1/2?curriculum_id=4f89dab3d788890003000096#


I don't understand why we can jump around from M and N to X and Y.
Seems a bit messy to me.


m = 5
n = 13
# Add add_function here!
def add_function(x,y):
    return x+y


print add_function(m, n)


#2

m and n are global variables, x and y are local to the function and cannot be seen from the outside.

The function call,

add_function(m, n)

passes the values (arguments) referred by (m, n) to the parameters, (x, y) which are copies of those values. The math is then done on those values and returned.

The order in which the values are written in the argument is the order in which they are assigned to the local variables. x becomes the value of m, and y of n. Neither m nor n are affected. The variables x and y are destroyed when the function is exited. Since the return value is printed immediately, it is also gone from memory. To store it,

my_sum = add_function(m, n)
print my_sum

If we only ever want to print then your example is the way to go rather than creating variables that are never used again. It prevents memory leakage (memory that is not garbage collected) and is easier to maintain.


#3

Fantastic explanation. Thanks a ton! I have been wondering why it seems like sometimes we have to RETURN and sometimes we don't.

I have a little better understanding of that now as well.

If you have some time to elaborate on the last two lines just a little more that would be dope!

Thanks again!

Much love


#4

Which two lines are you referring to?


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.