- One of the best tools in a programmer's tool belt
for improving the readability of their programs
is functions.
In the same way that variables
allow us to store individual pieces of data
to be reused throughout the program,
functions allow us to reuse behavior.
Function definitions in Python and JavaScript
look fairly different.
However, there are a lot of similarities.
In both Python and JavaScript
functions are considered first-class objects,
which means they can be assigned to variables,
pass as arguments too and return from functions.
Here's an example of a simple function
that adds two numbers in Python
and the same function in JavaScript.
In both cases, the parameters of the function
are defined within parentheses and separated by commas.
In both cases, the function is invoked or called
in the exact same way.
However, like other code blocks,
the body of the function is defined in Python
using significant whitespace,
while in JavaScript you'll use curly brackets.
Although they are defined slightly differently,
functions serve the same purpose in JavaScript and Python
and will bring more readability to your programs.