๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐ ๐๐ ๐ฝ๐น๐ฎ๐ถ๐ป๐ฒ๐ฑ
Functions are blocks of code. They perform specific tasks. You write them once and use them many times.
Functions do nothing until you call them. Calling a function means you tell the computer to run that specific code block.
Why you need functions:
- You reuse code without rewriting it.
- You organize code into small parts.
- You make your code easier to read.
- You maintain your code better.
How a function works: Functions use the function keyword. You give the function a name. You use parentheses () to hold inputs. You use curly brackets {} to hold the code.
Input and Output:
- Parameters: These are the values you send to a function.
- Arguments: These are the actual values the function receives.
- Return: This is the value the function sends back to you.
You can store the result of a function in a variable.
Example: function add(a, b) { return a + b; }
let sum = add(5, 5);
Local Variables: Variables created inside a function stay inside that function. This is called local scope.
- You cannot use a local variable outside its function.
- The computer creates the variable when the function starts.
- The computer deletes the variable when the function ends.
This keeps your code clean and prevents errors in other parts of your program.
Source: https://www.w3schools.com/js/js_function_intro.asp
Full post: https://dev.to/vidhya_murali_5aabe7784bd/functions-in-javascript-38h8