DEV Community

Mahalakshmi C
Mahalakshmi C

Posted on

Function in JavaScript - part1

Functions

Functions are one of the most important concepts in JavaScript.

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

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

For example, instead of writing console.log("Welcome") multiple times, we can create a function.

function greet() {
    console.log("Welcome");
}
Enter fullscreen mode Exit fullscreen mode

The code inside the function will not execute until the function is called.

greet();
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome
Enter fullscreen mode Exit fullscreen mode

Why Do We Use Functions?

Functions help us avoid repeating the same code. They also make programs more organized, reusable, and easier to understand.

For example, without a function:

console.log("Welcome, John");
console.log("Welcome, David");
console.log("Welcome, Mary");
Enter fullscreen mode Exit fullscreen mode

Using a function:

function greet() {
    console.log("Welcome");
}

greet();
greet();
greet();
Enter fullscreen mode Exit fullscreen mode

A function can be called multiple times whenever the same task needs to be performed.

Function Declaration

A function declaration is one of the most common ways to create a function in JavaScript.

The syntax is:

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

A function declaration contains:

  • The function keyword.
  • A function name.
  • Parentheses ().
  • Curly braces {} containing the function code.

For example:

function showMessage() {
    console.log("Learning JavaScript");
}
Enter fullscreen mode Exit fullscreen mode

Here, showMessage is the name of the function.

Calling a Function

Creating a function does not automatically execute its code. We need to call or invoke the function to execute it.

A function is called by writing its name followed by parentheses.

function showMessage() {
    console.log("Learning JavaScript");
}

showMessage();
Enter fullscreen mode Exit fullscreen mode

Output:

Learning JavaScript
Enter fullscreen mode Exit fullscreen mode

Here, the function is declared first and then called using showMessage().

Calling a Function Multiple Times

One of the main advantages of functions is reusability. We can call the same function multiple times.

function welcome() {
    console.log("Welcome to our website");
}

welcome();
welcome();
welcome();
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to our website
Welcome to our website
Welcome to our website
Enter fullscreen mode Exit fullscreen mode

The same block of code is executed every time the function is called.

Parameters

Parameters are variables that are written inside the parentheses of a function. They allow a function to receive values when it is called.

For example:

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

Here, name is a parameter.

The function can receive different values through this parameter.

greet("John");
greet("David");
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, John
Hello, David
Enter fullscreen mode Exit fullscreen mode

The function uses the value passed to it and produces different results.

Multiple Parameters

A function can have more than one parameter. Multiple parameters are separated by commas.

For example:

function introduce(name, city) {
    console.log(name + " is from " + city);
}

introduce("John", "Chennai");
Enter fullscreen mode Exit fullscreen mode

Output:

John is from Chennai
Enter fullscreen mode Exit fullscreen mode

Here, name and city are parameters.

Arguments

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

For example:

function add(a, b) {
    console.log(a + b);
}

add(10, 20);
Enter fullscreen mode Exit fullscreen mode

Here:

  • a and b are parameters.
  • 10 and 20 are arguments.

Output:

30
Enter fullscreen mode Exit fullscreen mode

Parameters vs Arguments

Parameters Arguments
Variables defined in the function declaration Actual values passed when calling the function
Receive values inside the function Provide values to the function
Example: function add(a,b) Example: add(10,20)
a & b are parameter 10 and 20 are an argument

The return Statement

The return statement is used to send a value back from a function.

For example:

function add(a, b) {
    return a + b;
}

let result = add(10, 20);

console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Here, the function calculates a + b and returns the result.

The returned value is stored in the result variable.

return vs console.log()

console.log() displays a value in the console, while return sends a value back from the function.

For example:

function add(a, b) {
    console.log(a + b);
}

add(10, 20);
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

In this example, the result is only displayed.

Now consider:

function add(a, b) {
    return a + b;
}

let result = add(10, 20);

console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Here, the result is returned from the function and can be stored in a variable or used in another operation.

For example:

function add(a, b) {
    return a + b;
}

let total = add(10, 20) * 2;

console.log(total);
Enter fullscreen mode Exit fullscreen mode

Output:

60
Enter fullscreen mode Exit fullscreen mode

This is one of the main advantages of using return.

Important Point About return

When JavaScript reaches a return statement, the function stops executing.

For example:

function check() {
    console.log("Start");

    return "Completed";

    console.log("End");
}

console.log(check());
Enter fullscreen mode Exit fullscreen mode

Output:

Start
Completed
Enter fullscreen mode Exit fullscreen mode

The code after the return statement is not executed.

Conclusion

Functions help us organize and reuse code in JavaScript. A function is created using the function keyword and is executed when it is called. Functions can receive values through parameters and arguments, and they can send results back using the return statement.

Understanding function declarations, function calls, parameters, arguments, and return values is important because these concepts form the foundation for working with functions in JavaScript.

Top comments (0)