F is For Function?

It has been a while since my last post, two (2) months to be exact, and while I haven’t been tracking my journey via this blog(or anywhere else for that matter), I’m happy to report that I have continued, albeit rather SLOWLY. I also got sidetracked and jumped down the rabbit holes that are CSS and HTML, which were a lot deeper than I expected. But now that I’m back on track, I think it’s only right that a recap of the completed topics is done.

We’ll start off with Functions, which is basically a chunk of code encapsulated in a binding. I learned that there are different ways to create a function:

  • The most common is similar to the declaration of a variable.

const squareNum = function(num) {
return num * num;
  • Declaration Notation

function squareNum(num) {
return num * num;

With this method, the function won’t follow the usual top to bottom flow of control and as such the function can be called before it is defined and your code will still run

  • Arrow Functions

const squareNum = (num) => {
    return num * num;

Which is basically saying if you put this in, the parameters, you’ll elicit this response, the executable statement.

An important feature of a function that should be highlighted is the Optional Argument. With this feature, functions can be created with arguments that are optional. A default value can also be given to an argument in the event that the user doesn’t provide one.

function demoFunction(a, b= default value){
executable statement;

If a value for “b” is not provided, the default value assigned to “b” will be used to execute the code in the body of the function.

Another subtopic of note for functions is scopes. How can we differentiate between local & global scopes and how will they affect your code. From what I understand, the local is confined to the function or block of code in which it resides while the global scope can be seen anywhere (meaning within the functions or blocks it is not apart of).

And now for my least favourite part of functions, recursion. A recursive function is one that calls itself repeatedly until a result is achieved (the stipulated stopping point is reached).

function isEven(n) {
    if (n == 1) {
        return "It is odd."
    } else if (n == 0) {
        return "It is even"
    } else if (n < 0) {
        return isEven(n * -1)
    } else {
        return isEven(n - 2)

Grasping this particular concept took me a while and the best explanation I could find for it was this YouTube video.

I hope this will give you a fairly basic understanding of functions; how they can eliminate repetition and organize your program into different sections based on their, well….function.

I will be following up on this blog post to shed some more light on the sometimes complex code blocks we call functions.

No Comments Yet