JavaScript Functions Explained

JavaScript Functions Explained

ยท

3 min read

What is functions ?

Functions are used when we want to repeat a particular action many number times ie: reusable code.

JavaScript Function Syntax

Function names can contain letters, digits, and much more. They just follow the same rules as variables. The parentheses may include parameter names separated by commas: (para1, para2, ...)

The code to be executed, by the function, is placed inside curly brackets: {}

function sayHi(name){
    console.log(`${name}`)
}
sayHi("Kaarthik"); // logs kaarthik in to console
  1. Parameters - Parameters are nothing but named variables passed into a function.
  2. Arguments - The arguments are the values passed into a function.
  3. Code - Code to be executed in the function placed inside the curly braces.
  4. Return - Return statement ends the function and returns control to the calling function. It returns value to the calling function.

Every function will gives us a value in back we can either log that in console or we can use the value using return keyword.

function fruitProcessor(apple, orange) {
    console.log(apple, orange);
    const juice = `Juice with ${apple} apples & ${orange} oranges`
    return juice;
}
let processedJuice = fruitProcessor(5, 0);
console.log(processedJuice);

Invoking a function

We cannot just expect output just by defining a function, We have to call or invoke the functions. We can do that in this way,

fruitProcessor(5,0)

Few Types of Functions

Function Expression:

A Function Expressions defines a named or anonymous function. An anonymous function is a function that has no name.

var fullName = function(firstName, lastName) {
 return `${firstName} ${lastName}`;
}
fullName("Kaarthik", "Sekar"); // Kaarthik Sekar

Arrow Functions:

Arrow Functions gives us a way to write functions in much simpler way.

const calcAge3 = birthYear => 2021 - birthYear;
const age3 = calcAge3(2001);
console.log(age3); // 20

One parameters and multiple statements:

This function contains one parameters and multiple statements ie : birthyear and few statements

const yearsUntilRetirement = birthYear => {
    const age = 2037 - birthYear;
    const retirement = 65 - age;
    return retirement;
}
console.log(yearsUntilRetirement(1991));

Multiple parameters:

This simple function contains multiple parameters ie: birthYear & firstName

const yearsUntilRetirement = (birthYear, firstName) => {
    const age = 2037 - birthYear;
    const retirement = 65 - age;
    // return retirement;
    return `${firstName} retires in ${retirement} years`;

}

console.log(yearsUntilRetirement(1991, "kaarthik"));

Function inside another function:

We have seen many types but another common use is using a function inside another function. We can call one separate function inside another function.

const cutter = (fruit) => {
    return fruit * 4;
}

const fruitProcessor = (apple, orange) => {
    let applePieces = cutter(apple);
    let orangePieces = cutter(orange);
    const juice = `Juice with ${applePieces} pieces of apple & ${orangePieces} piece of orange`
    return juice;
}
console.log(fruitProcessor(2, 3));

Conclusion

Once a function is defined, it can be used over and over and over again. You can invoke the same function many times in your program, which saves a lot of your work. So, Make use of functions <3


Thanks for reading my blog & Let's connect! ๐Ÿ™Œ๐Ÿผ

Follow me for more similar articles :D

Feel free to subscribe to my Youtube Channel & Follow me on Twitter <3

ย