๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐ ๐๐ ๐ฝ๐น๐ฎ๐ถ๐ป๐ฒ๐ฑ
Functions are blocks of code. They perform specific tasks. You write them once and use them many times.
Why you need them:
- You reuse code instead of rewriting it.
- You organize code into small parts.
- You make code easy to read and maintain.
How a function works:
You define a function with the function keyword. You give it a name. You add parentheses. You put the logic inside curly brackets.
A function does nothing until you call it. You call a function by using its name and parentheses. The parentheses tell the computer to execute the code now.
Syntax structure:
function name(parameter1, parameter2) { // code goes here }
Functions handle input and output:
- Parameters: These are the values you send to the function.
- Arguments: These are the actual values the function receives.
- Return: This is the value the function sends back to you.
You can store returned values in variables for later use.
Local variables:
When you declare a variable inside a function, it is local. This means only the code inside that function sees it. Other parts of your program cannot access it.
Local variables follow a simple lifecycle:
- They appear when the function starts.
- They disappear when the function ends.
This allows you to use the same variable name in different functions without causing errors.
Source: https://www.w3schools.com/js/js_function_intro.asp
Full post: https://dev.to/vidhya_murali_5aabe7784bd/functions-in-javascript-38h8