An Incredibly Simple, Basic, Bare-bones Explanation of Javascript Functions

As I get further and further into the Immersion Program I’m currently enrolled in , I find myself losing touch as to how inexplicably rough the beginning stages of learning to code were. A fellow student recently said “Please stop talking to me like i understand this stuff” so for this post, I won’t . I’ll attempt to explain functions in a way I wish someone would have for me.

Let’s start with what functions are. You’ll hear again and again and even again that functions are “reusable blocks of code that may take parameters and may accomplish some task and or return a value.” While that’s true, that definition doesn’t really say anything at all. If I were asked to describe what functions are to someone who has zero experience coding, I would simply say they are specialized tools because a shovel is all fine and dandy but with no dirt to dig they don’t really serve a purpose just like a function without data to act upon serves no purpose. Just like a shovel, you can only use functions for their intended purpose.

Above is a break down of a simple function “addStuff” which will return the sum of its parameters. However, it can be explained in even simpler terms.

  1. The keyword “Function” literally tells the computer you’re about to start writing a function.
  2. “addStuff” is a name I’ve given this function. Try to name functions in ways that indicate their purposes.
  3. Parameters such as “value1” and “value2” have zero value. They are just stand-in variable names for whatever input you feed to the function when you later “invoke”it, or run it later. It is good habit to name your parameters something representative of the kind of data that will normally be used with your function.
  4. The curly braces signify the body of the function.
  5. The return statement is the real meat and potatoes of a function — a function’s entire value resolves to its return statement.
  6. Finally we actually call/invoke/run/ our function. The values in the parenthesis are called arguments. These values serve as the input to our function.

I hope your main take away from this post is a clearer view of functions and a renewed confidence in your ability to learn to code. You won’t be writing the next viral app tomorrow but with time, practice, and bit of kindness to yourself, there is absolutely no reason why anyone can’t learn to code in Javascript.