๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐ ๐๐ ๐ฝ๐น๐ฎ๐ถ๐ป๐ฒ๐ฑ
Functions are blocks of code. You write them once and use them many times. They perform specific tasks.
Why you need them:
- They help you reuse code.
- They organize your code into small parts.
- They make your code easy to read.
How they work: A function sits idle until you call it. To run a function, use its name followed by parentheses. The parentheses tell the computer to execute the code now.
Syntax rules:
- Use the function keyword.
- Give the function a name.
- Add parentheses ( ) for inputs.
- Use curly brackets { } for the code logic.
Inputs and outputs: Functions work in a cycle.
- Parameters: You send values into the function.
- Arguments: The function receives these values.
- Logic: The function performs an action.
- Return: The function sends a result back to you.
You can store the result of a function in a variable.
Local variables: Variables created inside a function stay inside that function. This is called local scope.
- You cannot access these variables from outside the function.
- The computer creates them when the function starts.
- The computer deletes them when the function ends.
- This allows you to use the same variable names in different functions without errors.
Source: https://www.w3schools.com/js/js_function_intro.asp
Full post: https://dev.to/vidhya_murali_5aabe7784bd/functions-in-javascript-38h8
Optional learning community: https://t.me/GyaanSetuAi