DEV Community

Raja B
Raja B

Posted on

Function

Function:

  • A Function is a block of code written to perform a specific task.

  • Instead of writing the same code multiple times, we can place it inside a Function and call it whenever needed.

Funtion

1. Parameters (In Function Definition):

Parameters are the variables written when creating a function

function biriyani(rice, masala, chicken) {
    console.log(rice, masala, chicken);
}
Enter fullscreen mode Exit fullscreen mode

Here:

rice, masala, chicken
Enter fullscreen mode Exit fullscreen mode

These are called Parameters

2. Arguments (In Function Call):

Arguments are the actual values passed when calling a function

biriyani("Basmati Rice", "Spicy Masala", "Chicken");
Enter fullscreen mode Exit fullscreen mode

Here:

"Basmati Rice"
"Spicy Masala"
"Chicken"
Enter fullscreen mode Exit fullscreen mode

These are called Arguments

    function eat(food) {   // food = Parameter
    console.log(food);
}

    eat("Biriyani");       // "Biriyani" = Argument
Enter fullscreen mode Exit fullscreen mode

Parameter = In the function definition

Argument = In the function call

return

Return:

  • returnis used to send a value out from a function or return sends a value back from a function
       function biriyani(riceContainer, container, masalaContainer)
        {
            console.log(riceContainer, container, masalaContainer);
            console.log("biriyani ready");
            return "ChickenBiriyani";
        }

        function eat(food) 
       {
            console.log(food);
        }

        biriyani('rice', 'masala', 'chicken');
        eat();
Enter fullscreen mode Exit fullscreen mode

Output:

output

1. Function With Arguments:

biriyani('rice', 'masala', 'chicken');
Enter fullscreen mode Exit fullscreen mode
  • riceContainer, container, masalaContainerParameters

  • 'rice', 'masala', 'chicken'Arguments

The biriyani() function receives values when it is called, so it is a Function With Arguments

2. Function With Return Value:

return "ChickenBiriyani";
Enter fullscreen mode Exit fullscreen mode

The biriyani() function returns a value ("ChickenBiriyani"), so it is a Function With Return Value

3. Function Without Arguments:

eat();
Enter fullscreen mode Exit fullscreen mode

The eat() function is called without passing any value, so it is a Function Without Arguments

However:

function eat(food) {
    console.log(food);
}
Enter fullscreen mode Exit fullscreen mode

This function has a parameter (food), but you called it without an argument. Therefore food becomes undefined

Hoisting in JavaScript:

Hoisting means JavaScript moves declarations to the top of their scope before the code runs

console.log(name);

var name = "Raja";
Enter fullscreen mode Exit fullscreen mode

Output:

output1

JavaScript treats it like:

var name;

console.log(name);

name = "Raja";
Enter fullscreen mode Exit fullscreen mode

Output:

output2

Function Hoisting:

sayHello();

function sayHello() {
    console.log("Hello");
}
Enter fullscreen mode Exit fullscreen mode

Output:

output3

Because function declarations are hoisted completely

let and const:

console.log(age);

let age = 25;
Enter fullscreen mode Exit fullscreen mode

Output:

output4

Top comments (0)