Basic understanding of Functions
Published: 2022-06-23 16:47:30


Functions 

Have you ever had that feeling in life when so focused on learning something new, but as you learning your mind is trying to process so much it’s not making sense of the more complex information being passed on to you. Well believe me you not alone recently I have done what many of us say we hope to do and that is learn a language. I’m not here telling you I can now speak Spanish or Dutch but to my surprise I took up learning the language that software engineers speak JavaScript. 

Now when learning a language there are many different factors you must learn such as dialect or weather its past present or future. JavaScript is slightly different here you learn how it consists of so many different elements, I’m here to tell you what I found to be a vital element that will be used always think of it as one of the core elements.  That is you may ask…. A function. 


Functions are the single most important unit of code in JavaScript. This is because they serve as ways to group together related bits of JavaScript code. Grouped code is easier to read, debug, and improve. What must be know is that in order to "execute" or "call" a function in JavaScript you add () after its name.  when you call a function, it will need to return something.  Let’s think of ordering a meal if when I go to a restraint, I am hungry and I tell the waitress I would love a burger and if you think of this in a function the function would be placing the order and the return would be your burger.  However, this is not the case always Functions can, but are not obligated to, return return values at the end of their execution. Return values are often results of a process, grand totals, or success / failure data. So, it may be the case where go to a restaurant ask for a job but they are not able to give you one so you getting nothing in return. 

There are 3 ways in writing a function in JavaScript being Function Declaration, Function expression and arrow function 

Function Declaration  is the traditional way to define a function. It is somehow similar to the way we define a function in other programming languages. We start declaring using the keyword “function”. Then we write the function name and then parameters.


// Function declaration
function add(a, b) {        
    console.log(a + b);
}
 
// Calling a function
add(2, 3
Function Expression is another way to define a function in JavaScript. Here we define a function using a variable and store the returned value in that variable.


// Function Expression
const add = function(a, b) {
	console.log(a+b);
}

// Calling function
add(2, 3);


Arrow Function are been introduced in the ES6 version of JavaScript. It is used to shorten the code. Here we do not use the “function” keyword and use the arrow symbol.

// Function Expression
const add = function(a, b) {
    console.log(a+b);
}
 
// Calling function
add(2, 3);

Pretty simple to understand yeah, I know right but hey when is learning a language ever that easy.  so now I can tell where things may get a little complicated you know the basics of a function and what it does but what If I told you that you can pass a function within a function as an argument. Yup that’s right and the term for this is called nested. 

what we call this is a callback and a callback is a function that you pass into another function as an argument for executing later. Lets dive a little deeper with callbacks you get two types synchronous and also asynchronous. A synchronous callback is executed during the execution of the high-order function that uses the callback. The isOdd and isEven  are examples of synchronous callbacks because they execute during the execution of the filter() function. However lets switch that around cause An asynchronous callback is executed after the execution of the high-order function that uses the callback. A synchronicity means that if JavaScript must wait for an operation to complete, it will execute the rest of the code while waiting.

This is a video link to watch what a callback is in plain terms. 
https://youtu.be/AUCavCH7FTw

to sum this up and and not let you lose sense of what we have gone through today you should now know the basics of functions the type of functions and that you are able to have callback functions and through each of these to help you gain some insight to the basics of each use of a function. happy coding