Python Function Prototype

Viewed 980
def tempr():
        print("in the function")

.
.
.
.
tempr()

This way the python function works, while

tempr()
.
.
.
.
.
def tempr():
    print("in the function")

This does not work

while in c++ we have a prototype declaration to tell the compiler that the function exists. what do I do here? Can Python handle such kind of c++ kind of function call statement above the function definition

2 Answers

what do I do here?

Define or import the function before you call it. There's no way around it.

Can Python handle such kind of c++ kind of function call statement above the function definition

No, Python does not currently have function prototypes. As Iain Shelvington said, functions and variables are objects. (Literally, everything is an object in Python) As you must define a variable before use, same as functions. You could put the function in another file and import it if you don't want to see it...

The short answer: you have to follow a top-down structure in your code, and it's good practice. The way you have set it up is basically resulting in an undefined function being called as it has not even been setup in the memory stack (at run-time).

Related