๐— ๐—ผ๐—ฟ๐—ฒ ๐—”๐—ฏ๐—ผ๐˜‚๐˜ ๐—๐—ฆ ๐—™๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐˜€

Writing code without functions leads to duplication. You repeat the same logic many times. This makes your code hard to read and maintain.

Functions solve this problem. They allow you to write code once and use it many times.

Why use functions?

Naming Rules Use camelCase for function names. Start with a lowercase letter. Capitalize the first letter of every word after that.

Parameters vs Arguments People often confuse these two.

Parameters are variables in the function definition. They act as placeholders. Example: function add(a, b)

Arguments are the actual values you pass when you call the function. Example: add(10, 20)

The Return Statement A function can perform a task or return a value. Use return when you need to store a result or use it in another calculation.

If you do not use return, the function gives back undefined.

Note on multiple returns: You can have multiple return statements inside an if-else block. However, only one executes. Once a return runs, the function stops immediately.

Do not try to return multiple values with commas like this: return "Biriyani", 140, "Salem". JavaScript will only return the last item.

To return multiple pieces of data, use an array or an object:

Hoisting JavaScript moves function declarations to the top of their scope before running the code. This means you can call a function before you define it in your script.

Important Tip: JavaScript passes values, not variables. If you pass a number into a function and change it inside, the original number outside remains the same.

Source: https://dev.to/annapoo/more-about-function-in-js-5b19