DEV Community

Rakshambika
Rakshambika

Posted on

Functions in JS

What is Function?

  • A function is a reusable block of code that performs a specific task.

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

  • They can take inputs, perform actions, and return outputs.

Why do we use Functions?

  • Reduce code repetition
  • Improve code reusability
  • Make programs easier to read and maintain
  • Break large problems into smaller tasks

Syntax:

function functionName() {
    // code to execute
}
Enter fullscreen mode Exit fullscreen mode

Example:

function addtwonumbers(a,b)
{
let total= a+b;
console.log(total);
}
addtwonumbers(10,30);
Enter fullscreen mode Exit fullscreen mode

Output:

40
Enter fullscreen mode Exit fullscreen mode

Parts of Function:

function addtwonumbers(a, b) {
    let total = a + b;
    console.log(total);
}
Enter fullscreen mode Exit fullscreen mode
Part Code Explanation
Function Keyword function Used to declare a function.
Function Name addtwonumbers Identifies the function and is used to call it.
Parameters a, b Variables that receive values when the function is called.
Function Body { let total = a + b; console.log(total); } Contains the statements that execute when the function is called.
Local Variable total Stores the result of adding a and b. Accessible only inside the function.
Output Statement console.log(total) Displays the result in the console.

Function Call:

addtwonumbers(10, 30);
Enter fullscreen mode Exit fullscreen mode
Part Code Explanation
Function Call addtwonumbers(10, 30) Executes the function.
Arguments 10, 30 Actual values passed to the parameters a and b.

Parameters:

  • Parameters are variables declared in the function definition.
  • They act as placeholders that will receive values when the function is called.

Example:

function greet(name) {
    console.log("Hello " + name);
}
Enter fullscreen mode Exit fullscreen mode
function greet(name)
                ↑
            Parameter
Enter fullscreen mode Exit fullscreen mode

Here, name is a parameter.

Arguments:

Arguments are the actual values passed to a function when it is called.

Example:

greet("Raksha");
Enter fullscreen mode Exit fullscreen mode
greet("Raksha");
       ↑
    Argument
Enter fullscreen mode Exit fullscreen mode

Here, "Raksha" is an argument.

Parameters vs Arguments :

Both parameters and arguments are used to pass data to a function, but they are used at different stages.

Feature Parameters Arguments
Definition Variables defined in the function declaration. Actual values passed to the function when it is called.
Location Written inside the function declaration parentheses. Written inside the function call parentheses.
Purpose Receive data from the function call. Provide data to the function.
Example a, b 10, 30

Function with console.log() and return

Both console.log() and return can be used inside a function, but they have different purposes.

Function with console.log():

console.log() is used to display the value in the console.

Example:

function add(a,b)
{
console.log(a+b);
}
add(40,60);
Enter fullscreen mode Exit fullscreen mode

Output:

100
Enter fullscreen mode Exit fullscreen mode

Here, console.log(total) only prints the value. It does not send the value back to the calling code

Function with return:

return is used to send a value back from the function.

Example:

function subtract(c,d)
{
    return c-d;
}
let result=subtract(100,40);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

60
Enter fullscreen mode Exit fullscreen mode
  • Here, the value is stored in a variable.
  • The stored value is used later.

Example:

function subtract(c,d)
{
    return c-d;
}
let result=subtract(100,40);
console.log(result);

let a=result+200;
console.log(a);
Enter fullscreen mode Exit fullscreen mode
260
Enter fullscreen mode Exit fullscreen mode

Here, the value returned from a function is stored inside a variable result and used later.

So, return allows us to get a value from a function and use that value outside the function.


Top comments (0)