83
def sum_numbers(a, b):
    return a + b

def print_sum(a, b):
    print(sum_numbers(a, b))

Does the order in which the function are written matter? If I had written the print_sum function first and then the sum_numbers, would the code still work? If yes, does it always work?

6
  • 8
    "... would the code still work?" <-- it's literally faster to just try this part than ask about it. Whenever you want to ask "does thing work in python?", try it first. If the result baffles you, you'll be able to ask a more interesting and specific question. Commented Jun 8, 2017 at 10:24
  • 36
    @AndrasDeak The question may be simple, but that doesn't mean it's not worth asking. Questions like these can be helpful for newer programmers, or for when the answer can't otherwise be found from a quick search. Commented Mar 22, 2018 at 18:35
  • 3
    @StevenVascellaro I'm not sure I understand your point. New programmers can also literally try it faster than ask or read about it :) Commented Mar 22, 2018 at 18:40
  • 4
    @AndrasDeak However people that reach this question doesn't have exactly this code snippet in mind. Also note that the question actually consists of two where the second is not as simple to check as the first. Also note that in general this type of question is not possible to check if there's input involved (that would in general need to solve the stopping problem). Commented May 30, 2019 at 7:38
  • 5
    I want to add (years later) that just because something works when you try it, doesn't mean that it's supposed to work. It could just be a coincidence or a bug; a quirk of your script's environment. It is worthwhile to look these things up and ask these questions. Commented May 28, 2024 at 15:12

3 Answers 3

125

The only thing that Python cares about is that the name is defined when it is actually looked up. That's all.

In your case, this is just fine, order doesn't really matter since you are just defining two functions. That is, you are just introducing two new names, no look-ups.

Now, if you called one of these (in effect, performed a look-up) and switched the order around:

def print_sum(a, b):
    print(sum_numbers(a, b))

print_sum(2, 4)

def sum_numbers(a, b):
    return a + b

you'd be in trouble (NameError) because it will try to find a name (sum_numbers) that just doesn't exist yet.

So in general, yes, the order does matter; there's no hoisting of names in Python like there is in other languages (e.g JavaScript).

Sign up to request clarification or add additional context in comments.

2 Comments

Does this ultimately have to do with the fact that Python is an interpreted rather than compiled language?
Python Docs: "The function definition does not execute the function body; this gets executed only when the function is called."
39

It doesn't matter in which order the functions are created. It only matters when the call to the function is done:

def print_sum(a, b):
    print(sum_numbers(a, b))

def sum_numbers(a, b):
    return a + b

print_sum(1, 3)
# 4

that works because at the time print_sum is called both functions do exist. However if you call the function before defining sum_numbers it would fail because sum_numbers isn't defined yet:

def print_sum(a, b):
    print(sum_numbers(a, b))

print_sum(1, 3)

def sum_numbers(a, b):
    return a + b

throws:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-34-37c0e3733861> in <module>()
      2     print(sum_numbers(a, b))
      3 
----> 4 print_sum(1, 3)
      5 
      6 def sum_numbers(a, b):

<ipython-input-34-37c0e3733861> in print_sum(a, b)
      1 def print_sum(a, b):
----> 2     print(sum_numbers(a, b))
      3 
      4 print_sum(1, 3)
      5 

NameError: name 'sum_numbers' is not defined

2 Comments

To be nitpicking there actually are cases where the order matters. If a symbol is being looked up as part of the definition/creation of the object the symbol must be defined earlier. An easy example is if you've got default argument of a function, those are evaluated at definition time). Another example would be inheritance where the base classes must be defined first.
Why not making an answer from your comment @skyking ?
1

It doesn't matter in which order functions are defined as shown below:

def display():
    print(text())

def text():
    return "Hello World"

display() # "Hello World" is displayed

But, it matters where functions are called so if calling "display()" at the beginning as shown below:

display() # Error

def display():
    print(text())

def text():
    return "Hello World"

Then, there is the error as shown below:

Traceback (most recent call last): File "", line 1, in NameError: name 'display' is not defined

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.