Functions in JavaScript | JavaScript Made Easy - Session 19

Functions in JavaScript | JavaScript Made Easy - Session 19

Brief Summary

This video provides an introduction to functions in JavaScript, explaining what they are, how to create them, how to pass parameters, and how to return values. It covers the syntax of a function, the concept of function calling, and the use of the return keyword.

  • Functions are blocks of code designed to perform specific tasks.
  • Functions can accept parameters as input and return values as output.
  • The return keyword is used to send a value back to the function calling statement.

What is a Function?

A function is a block of code designed to perform a specific task. The syntax of a function in JavaScript includes the function keyword, followed by the function name, circular brackets (), and curly braces {} to define the function's body. Inside the curly braces, you can write any number of statements that the function will execute. For example:

function sampleFun() {
  // Code to be executed
}

This block of code is what constitutes the function. The function keyword is essential for creating a function in JavaScript.

Creating and Calling Functions

To create a function, you use the function keyword followed by the function name and parentheses. For example, function sampleOne() {}. Inside the curly braces, you can write any number of lines of code. However, the code inside a function will not execute unless the function is called. To call a function, you use the function name followed by parentheses, like so: sampleOne();. You can call a function multiple times, and each time it is called, the code inside the function will be executed.

Functions with Parameters

Functions can accept parameters, which are variables listed inside the parentheses in the function declaration. For example, function demoOne(name) {} defines a function that accepts one parameter named name. When calling a function with parameters, you need to pass arguments, which are the actual values that will be assigned to the parameters. For example, demoOne("Arun Motor"); passes the argument "Arun Motor" to the name parameter. If you don't pass any data to a parameter, JavaScript will assign undefined to it.

Functions with Multiple Parameters

Functions can have multiple parameters, separated by commas. For example:

function add(a, b) {
  let sum = a + b;
  console.log("Sum is " + sum);
}

In this case, the function add has two parameters: a and b. When calling the function, you need to provide arguments for each parameter, like so: add(1, 2);. Similarly, you can create a subtraction function with two parameters:

function subtraction(x, y) {
  let result = x - y;
  console.log("Subtraction is " + result);
}

You can have any number of parameters in a function, depending on your requirements.

Returning Values from Functions

Functions can return values back to the function calling statement using the return keyword. For example:

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

In this case, the function add calculates the sum of a and b and then returns the result. The returned value can be stored in a variable or directly used in a console.log statement. For example:

let result = add(3, 4);
console.log(result); // Output: 7

Alternatively, you can directly call the function inside the console.log statement:

console.log(add(3, 4)); // Output: 7

Functions can only return one value, but they can have any number of parameters.

Watch the Video

Share

Stay Informed with Quality Articles

Discover curated summaries and insights from across the web. Save time while staying informed.

© 2024 BriefRead