in , ,

JavaScript Function and Function Expressions

JavaScript Function and Function Expressions
JavaScript Function and Function Expressions

JavaScript Function

In this tutorial, you will learn about the JavaScript function and function expressions with the assistance of examples.

In this article, you will learn-

Why Functions?

You can reuse code: Define the code once, and use it ordinarily.

You can use a similar code commonly with various arguments, to produce various outcomes.

JavaScript Function

A function is a block of code that plays out a particular task.

Assume you need to make a program to make a loop and shading it. You can make two functions to tackle this issue:

  • a function to draw the circle
  • a function to color the circle

Dividing a perplexing issue into smaller chunks makes your program straightforward and reusable.

JavaScript additionally has a colossal number of inbuilt functions. For instance, Math.sqrt() is a function to calculate the square root of a number.

In this tutorial, you will learn about user-defined functions.


Declaring a Function

The syntax to declare a function is:

function nameOfFunction () {
    // function body   
}
  • A function is announced using the function watchword.
  • The essential standards of naming a function are like naming a variable. It is smarter to write an elucidating name for your function. For instance, if a function is used to add two numbers, you could name the function add or addNumbers.
  • The body of function is written within {}.

For instance,

// declaring a function named greet()
function greet() {
    console.log("Hello there");
}

Calling a Function

In the above program, we have proclaimed a function named greet(). To use that work, we need to call it.

Here’s the way you can call the above welcome() work.

// function call
greet();

Example 1: Display a Text

// program to print a text
// declaring a function
function greet() {
    console.log("Hello there!");
}

// calling the function
greet();

Output

Hello there!

Function Parameters

A function can likewise be pronounced with parameters. A parameter is a worth that is passed while pronouncing a function.


Example 2: Function with Parameters

// program to print the text
// declaring a function
function greet(name) {
    console.log("Hello " + name + ":)");
}

// variable name can be different
let name = prompt("Enter a name: ");

// calling function
greet(name);

Output

Enter a name: Salman
Hello Salman 🙂

In the above program, the greet function is pronounced with a name parameter. The user is incited to enter a name. At that point when the function is called, a contention is passed into the function.

Note: When a worth is passed while proclaiming a function, it is called a parameter. What’s more, when the function is called, the worth passed is called an argument.


Example 3: Add Two Numbers

// program to add two numbers using a function
// declaring a function
function add(a, b) {
    console.log(a + b);
}

// calling functions
add(3,4);
add(2,9);

Output

7
11

In the above program, the add function is used to find the sum of two numbers.

  • The function is declared with two parameters a and b.
  • The function is called using its name and passing two arguments 3 and 4 in one and 2 and 9 in another.

Notice that you can call a function the same number of times as you need. You can write one function and afterward consider it on numerous occasions with various contentions.


Function Return

The return statement can be used to return the value to a function call.

The return statement denotes that the function has ended. Any code after return is not executed.

If nothing is returned, the function returns an undefined value.


Example 4: Sum of Two Numbers

// program to add two numbers
// declaring a function
function add(a, b) {
    return a + b;
}

// take input from the user
let number1 = parseFloat(prompt("Enter first number: "));
let number2 = parseFloat(prompt("Enter second number: "));

// calling function
let result = add(number1,number2);

// display the result
console.log("The sum is " + result);

Output

Enter first number: 3.4
Enter second number: 4
The sum is 7.4

In the above program, the sum of the numbers is returned by the function using the return statement. What’s more, that worth is stored in the outcome variable.


Benefits of Using a Function

  • The function makes the code reusable. You can declare it once and use it multiple times.
  • The function makes the program easier as each small task is divided into a function.
  • The function increases readability.

Function Expressions

In Javascript, functions can also be defined as expressions. For example,

// program to find the square of a number
// function is declared inside the variable
let x = function (num) { return num * num };
console.log(x(4));

// can be used as variable value for other variables
let y = x(3);
console.log(y);

Output

16
9

In the above program, variable x is used to store the function. Here the function is treated as an articulation. Furthermore, the capacity is called using the variable name.

The function above is called anonymous function.

Note: In ES2015, JavaScript articulations are written as arrow works. You will learn about them in later tutorials.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

How to Recall Sent Emails in Outlook

The most effective method to Recall Sent Emails in Outlook

How to Check the CPU Temperature in Windows 10

Instructions to Check the CPU Temperature in Windows 10