Welcome back to a new sub edition of our Javascript course, as we take on one of the most fundamental concepts in Javascript. Yeah you guessed right ,oh sorry you😄peeped right from the heading it's FUNCTION you know 🤭.

Enough of the gimmicks, What's a Function???

Functions are like containers that hold some lines of codes that we can parse arguments in to them and give back a result in return.

It's mostly employed when we have a piece of code that we want to run a lot of times , Invariably, it's a functioning block of code that carry out functional functionality 🤭🤭 in JavaScript ,(The Language of interactivity).

Looks like we're really pumped up for today's discuss ?

So let's leverage on that energy to explain how a function is declared and what we need to take into consideration.

Firstly, a function in JavaScript is initialised with the function keyword, followed by the function name (like we do for variables after the popular var keyword) ****after which a parentheses would be opened, either to remain empty or to accommodate arguments/parameters as the case may be. And lastly followed by curly braces to house the piece of code, otherwise known as the function block.

See illustration below

            ↗️ //function name
 function checkDate(){
                    ↘️ parentheses to
   ↘️                 accomodate args
//function block

So, after we've known how to setup a function, Let's quickly set a working function below

function checkAge(birthYear){
 return 2021-birthYear
var age = checkAge(1997);

 // console prints 24 (2021-1997).

Let's explain this, it's somehow looking like a shady deal 😥

Yea, we setup the function as usual, we parsed in an argument of birthYear, in the function block , we employed a new keyword as far as this discuss is concerned i.e return (which simply works according to its name, it returns an output (the operation assigned to it / stated after it). Here , the operation we aimed at returning is the difference between the current year(2021) and the birthYear, mind you same as the arg passed into the parentheses because we would be in need of the birth year(a varying entry) to calculate for different persons unlike the current year which is definite.

Outside the function block , we saved a test-code by calling our function while assigning to it a parameter (birthYear) into a variable tagged age after which we passed it into the console to print a result.

Meaning we can use the function by simply calling it i.e checkAge() while passing in the test brithYear and the person's age would be determined.

Before we call it a day, let us work with one more function (a little bit complex) than the previous one.

function yearsTillRetirement(firstName,birthYear){
  var age = checkAge(birthYear) ; // checkAge()
                                    called back 
  var retirement = 65-age;
if(retirement > 0){
console.log( firstName + 'has' + retirement + ' years to retire')
console.log( firstName + 'has retired');
yearsTillRetirement(Cr7 , 1990) 
// Cr7 has 34 years to retire
yearsTillRetirement(Pele  , 1952 ) 
//Pele has retired

So good day.. that's all. 👋👋

No, we would need to really sort out this shii right?? 🤭