Functions :

In JavaScript, functions are reusable blocks of code that can be defined and invoked to perform specific tasks. They are an essential part of the language and provide a way to organize and encapsulate logic.
Function takes input, do some maipulations on input and then gives back output with a return statement.

For understanding how to write functions, we need to understand 3 things: function declarations, function expressions. and function invocation/call

Function Declarations:
A function declaration defines a named function using the function keyword. It has the following syntax

function functionName(parameters) {
  // code to be executed
}


Here's an example of a function declaration:

function greet(name) {
  console.log("Hello, " + name + "!");
}


In this example, the greet function is declared with a single parameter name. When invoked, it prints a greeting message to the console, using the provided name.

Function Expressions:
A function expression assigns a function to a variable. It has the following syntax:

const functionName = function(parameters) {
  // code to be executed
};


Here's an example of a function declaration:

const square = function(num) {
  return num * num;
};


In this example, the square function is defined as an anonymous function and assigned to the square variable. It takes a single parameter num and returns the square of the input value.

Function Invocation/call:
Once a function is defined, it can be invoked (or called) by using its name followed by parentheses () and passing any required arguments. Here's an example:

greet("Alice"); // Output: Hello, Alice!

const result = square(5);
console.log(result); // Output: 25


In this example, the greet function is called with the argument "Alice", resulting in the greeting message being printed to the console. The square function is called with the argument 5, and the returned value (25) is stored in the result variable, which is then printed to the console.

Functions in JavaScript can also have optional parameters, default parameter values, and can return values using the return statement. Additionally, they can be defined inside other functions (nested functions) and can be passed as arguments to other functions (higher-order functions).

Functions play a crucial role in JavaScript programming as they enable code reuse, modularization, and the ability to organize complex logic into manageable units. They are fundamental to building applications in JavaScript.

Function parameters vs arguments :

In JavaScript, function parameters and arguments are related concepts that refer to the values passed into a function during its invocation. Parameters are the variables listed in the function declaration, while arguments are the actual values passed to the function when it is called.

// function defination
function sayHello(name, age) {
  console.log("Hello, " + name + "! You are " + age + " years old.");
}

// function call
sayHello("Alice", 25);


In the above function defination code, name and age are the parameters.

While in the function call code, "Alice" and 25 are the arguments (that is tha actual value we are passing in the function body).

Returning values from functions :

Returning values from functions in JavaScript allows the functions to produce a result or output that can be used in other parts of the program.

The return statement is used to specify the value that should be returned from a function.

When a return statement is encountered in a function, it immediately terminates the function execution and sends the specified value back to the caller.

Here's an example that demonstrates returning a value from a function:

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

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


Using the return statement, the add function in this example returns the sum of the two inputs, a and b. The return a + b; instruction is executed and the number 7 is returned when the function is called with inputs 3 and 4. The result variable then holds the returning value and prints it to the terminal.

Functions can return any valid JavaScript value, including numbers, strings, booleans, objects, arrays, or even other functions. You can also return complex data structures or perform calculations or operations before returning a value.

It's important to note that when a return statement is encountered, it immediately exits the function, skipping any code that follows it within the same block. This behavior allows you to control the flow of execution and determine when a function should stop and provide a result.