Kanishk
Kanishk

Reputation: 1

Can we use a function in Python before even declaring it?

I'm getting errors while declaring a simple 'sum of two numbers' function if I'm using it before its declaration in Python 3.8.0

I tried both ways, declaring the function before and after its usage. In the case when I declared the function before its usage (to be specific, at the top of my code), my program is simply running fine. But when I tried to declare the sum function after its usage (in this case, at the extreme bottom of the code), it gave me an error : " 'int' object is not iterable ".

m = sum(var1,var2)   # usage of the function
print(m)

# here, the function is declared at the bottom, i.e., after the usage.
# the error is in line 7 of the code

def sum(a,b):            # The function
    return int(a+b)


#IN CASE OF THE FUNCTION DEFINITION AFTER ITS USAGE, THE FOLLOWING ERROR IS COMING:

Traceback (most recent call last):
  File "C:/Users/yvish/AppData/Local/Programs/Python/Python38/sum.py", line 7, in <module>
    m = sum(var1,var2)
TypeError: 'int' object is not iterable

Upvotes: 0

Views: 67

Answers (3)

Toothpick Anemone
Toothpick Anemone

Reputation: 4636

sum is a built-in function. It accepts a single "iterable" (container) as input.
For example, [1, 1, 1, 1, 2] is an iterable. The following code prints "6":

lyst = [1, 1, 1, 1, 2]
print(sum(lyst)) # prints `6`

lyst = [2, 4]
print(sum(lyst)) # prints `6`

lyst = [2, 2, 2]
print(sum(lyst)) # prints `6`

The next bit of code...

sum(3, 4)

is invalid because an int, such as 3, is not a container of numbers -- it is one number only.


You talk about "using" a function in Python before even declaring it. Rather than "use," the proper terminology is "call." Also, "declare" and "define" mean different things, but let's not get into that.

def foo(x, y)     
    return x + y  

print(foo(1, 2))   # This is a function "call" 

The answer is no, You cannot call a function in Python before defining it. The call must appear sometime after the function definition.

Upvotes: 1

David Maze
David Maze

Reputation: 159592

In the form you've shown it, you can't do this, because the Python interpreter runs through the script in order; it tries to execute your function before you get to the statement executing it.

It's usually good practice to put all of your code in functions, and maybe put one function call at the end. Mechanically, the interpreter will execute one def call, then the next one, and then try to run the code only once it's actually called.

def main():
  m = add_up(var1,var2)
  print(m)

def add_up(a,b):
  return int(a+b)

if __name__ == '__main__':
  main()

You're getting the specific error you are because sum is a built-in function. It's not that it's not defined yet, it's that it is defined but as a function that takes a single list.

def main():
  m = sum([var1, var2])
  print(m)

Upvotes: 0

James
James

Reputation: 36721

sum is also a built-in function. So you are calling a built-in function at the top, which is giving you the error as sum expects an iterable. Then you are redefining sum to be something else later in your script.

To answer you question: no, you cannot call a function and have the result returned before you define it in a script.

Upvotes: 0

Related Questions