An important designation here is that it is not the order in which the functions are created that matters, it matters when the call to the function is done.
Take for example the following code:
def add_one(new):
return my_add(new, 1)
def my_add(x, y):
return x + y
my_var = 2
print("The value of my_var is: {}".format(my_var))
my_var = add_one(my_var)
print("The value of my_var is: {}".format(my_var))
It yields
The value of my_var is: 2
The value of my_var is: 3
This is happening because by the time the add_one
function is called, both functions already exist. But if try and call add_one
before defining my_add
...
def add_one(new):
return my_add(new, 1)
my_var = 2
print("The value of my_var is: {}".format(my_var))
my_var = add_one(my_var)
print("The value of my_var is: {}".format(my_var))
def my_add(x, y):
return x + y
We get:
The value of my_var is: 2
Traceback (most recent call last):
File "c:\Users\wundermahn\Desktop\Stack.py", line 6, in <module>
my_var = add_one(my_var)
File "c:\Users\J39304\Desktop\Stack.py", line 2, in add_one
return my_add(new, 1)
NameError: name 'my_add' is not defined
See Does the order of functions in a Python script matter? for more