Javascript: Functions
Every software engineer or developer has a unique story about how they felt the very first time they fully comprehended the awesome powers that having ‘functions’ as a tool in your programming toolbox confers. Personally, it was like i never had to write any code more than ones within the same project ever again.
1kg is equivalent to 2.20 pounds.
Imagine for a moment that your boss had a Xkg bag of beans that weighed 40 pounds and he needs you to find out how much of kg is in 40pounds of weight.
The mathematical function for doing this would be:
xkg = (1/2.2) * value in lbs
Thats easy for one bag of beans. Now, imagine that you had to compute that for 1000 bags of beans! Thats where functions come in handy.
In Javascript, functions are one of the programming tools we use to automate processes and isolate the responsibility and requirements of each task from the next one within the same program or application.
Functions have names, can take inputs as attributes, perform actions on such inputs and return an output. You can define your own function like this:
let nameOfYourFunction = function( inputs ){
//function body
return output}
From the above, we defined a function that we have stored in a named variable called nameOfYourFunction. nameOfYourFunction is the name of our function and we have also used the function keyword to show that this is a function. Every function has a body and it is in this body that programmers specify programming instructions for the function. Although this is optional, functions can accept inputs as seen above. The programming instructions within the body of our function is what acts on the input to return an output which can be accessed whenever we call our function by name.
Imagine now that we want to write a function that helps us compute the equivalent weight in pounds for 50 bags of beans with 50 different weights in kg.
To begin, we need to choose a descriptive name for our function. Let’s call it ‘weightConverter’. Each time we are handed a bag with a kilogram value, we need to supply this kilogram value to our function as an input value when we call our weightConverter. let’s call this input ‘weightInKg’ which means that regardless of what value a kg weight is, when we pass it into our function as an input, a name tag with ‘weightInKg’ is stapled on it.
So, we have our function name, and an input which our function needs to work on and now its time to create the function:
let weightConverter = function( weightInKg ) {
//body of function
return output
}
Now, we have to basically multiply the input ‘weightInKg’ by 2.2 in order to get the equivalent weight in pounds, which our function would return as an output. Within our function body we need to pass these instructions to Javascript as shown below:
let weightConverter = function( weightInKg ) {
let output = weightInKg * 2.2
return output
}
Viola!
We have written the code ones and now i can use the function to convert all of our Uncles weights from Kg to pounds by simply calling my new function and passing in the weight in kg as an input to the function. Below is how we call the function and pass in our input.
let weightInPounds = weightConverter( 50 );
In the line above, weightConverter() calls our function, the 50 would be the value of the input we passed to our function.
NB: So, within our function body, weightInKg = 50. If our Uncle gave me a weight of 40kg, then my function call would be weightConverter(40), and weightInKg would then have a value of 40.
The output that our function call returns is what we then store in a new variable that we have called weightInPounds.
Functions in javascript are quite robust to an extent in that, in addition to the fact that we can feed in multiple inputs to our functions, we are allowed to also set default values on any of these inputs, by the way, these inputs have a special name, they are called attributes.
let weightConverter = function( weightInKg = 10 ) {
let output = weightInKg * 2.2
return output
}
From the above, we have passed in an input with a default value. So, whenever we call our function without passing in any values for weightInKg, the function would simply use 10 as the default value rather than undefined.