Why does this work?


Why does the following code work?
I'm obviously new to coding and kinda hung up that values(if I'm using the proper term) don't match. Why would return x + y add m + n to get the result?!? It seems so counter intuitive.

m = 5
n = 13

Add add_function here!

def add_function(x,y):
........return x + y

print add_function(m, n)


look at this code:

# function declaration with x and y parameter
def add_function(x,y):
    return x + y

# function call with m and n argument
print add_function(m, n)

did you read the comments? the arguments (m and n) are copied into the function parameters (x and y) the advantage of this method, is that you can call the function multiply times with different values:

m = 5
n = 13

def add_function(x,y):
    return x + y

print add_function(m, n)
print add_function(12,11)


x and y are called parameters. You can use other names, not just x and y. When you run the function, their values can vary.

m and n are arguments. You're calling the function with these.

Try these with your function:

a = 1
b = 2
print add_function(a, b)

# You can also call it with constants
print add_function(10, 20)

c = 3
d = 4
print add_function(c, d)

# You can combine both variables and constants
print add_function(c+5, d+10)

Play more and more with it. You'll soon get a hang of it :slight_smile:


Thanks for the structured answer. Still trying to wrap my head around it. A better understanding of the definitions would be probably be beneficial. If that was a lesson on its own(understanding what a function, parameter, etc.) I'm sure it would clear a lot of things up for beginners like myself.