Order and Position of Functions in Python
What Does an Interpreted Language Actually Mean?
A programming language contains instructions that follow certain
syntax rules, allowing us to program the computer to perform a
sequence of operations to obtain a
result. The instructions are written in a language close to natural language, most often in English, resulting in the
source code.
Depending on the language used, the source code is transformed into machine code using a
compiler or an
interpreter.
A
compiler scans and analyzes all the source code, then transforms it all into machine code in the form of an executable program. Although it is faster, the program errors are displayed at the end, making debugging a bit more difficult.
Examples:
C++,
C#,
Java, etc.
An
interpreter transforms the code into machine code line by line, without requiring an executable file generated at the end. The analysis stops at the first error, making it easier to debug programs.
Examples:
Python,
Perl,
JavaScript, etc.
The Python language
is interpreted, so when we run a program, it sequentially processes each command and
tries to execute them.
Where Can We Define a Function?
We cannot call a function that has not yet been defined!
The variable
a was assigned the value
23, then a nonexistent function was called. Therefore,
we create the
function first and then we can call it in the program:
Multiple Functions
When our programs become quite large,
the order of defining subprograms is important, because we might mistakenly position them as follows:
NOTES
The definition of the function
one was stored, then the variable
a was assigned the value
10.
The function
one is called, but at that moment, the definition of the function
two does not exist, resulting in an interpretation error!
Functions are not executed by the Python interpreter but are only stored as definitions in memory, so their order does
not matter,
as long as they are defined before being called! Try defining the function
two after
one and the program will work correctly!
Even in programming, things are very subtle. Anyone who says Python is easy is greatly mistaken, as you can see.
Many aspects need to be considered, theoretical concepts must be solidly understood, and they will undoubtedly provide
you with satisfaction in the coding world. You will come to know perfectly how code consisting of tens of thousands of lines works – your creation!
Don't forget about comments... someday they will matter!
This section is now over.