Ah, functions. We love them because they're the key to modular, don't-repeat-yourself (DRY) programs. The use cases in JavaScript and Python are the same: blocks of code intended to be called more than once, usually with varying parameters. Parameters are the variables that a function will take in order to execute its code on a mutable dataset. Arguments are what we pass when we call a function. They're the same thing in essence, but have different words depending on where and when they're used: are they the abstraction, or are they the actual data? Let's take a look at a side-by-side comparison:
Python | JavaScript |
def add_one(x): |
function addOne(val) { |
If you haven't already brought up the JavaScript console in your browser, you should do that now to see our output of 6.
You can see that the structure is fairly similar, with our...