Day:7- JS Functions

What is Function?

A Function is a reusable block of code designed to perform a specific task. Instead of writing the same 10 lines of code over and over again, you wrap those lines inside a function and run it whenever you need it.

Types of Functions

1. Function Syntax (Declaring & Calling)

To create a function, you use the function keyword, give it a custom name, add parentheses (), and place your code inside curly braces {}.

Creating a function is called Declaring it. To actually run the code inside it, you must Call (or invoke) it using its name followed by ().

2. Parameters vs. Passing Arguments

Parameters: These are the place-holder variable names defined inside the function parentheses () when declaring the function.

Arguments: These are the real, actual values you pass into the parentheses () when calling the function.

3. The return Keyword

By default, functions just perform actions (like console.log()). But usually, you want a function to calculate something and give you back the final result so you can store it in a variable or use it elsewhere.

The return keyword tells JavaScript: "Hand this final value back to whoever called the function, and stop running!"

⚠️ Important Rule: As soon as JavaScript hits a return statement, the function terminates immediately. Any code written below return inside that function will be ignored!