javascript-functions Flashcards
What is a function in JavaScript?
In JavaScript, a function is a block of code that is defined once and can be called any number of times to perform a specific task.
Describe the parts of a function definition.
1 The function keyword: This is the keyword that indicates that the code following it is a function definition.
2 The function name: This is the name of the function, which is used to identify the function and call it later on.
3 The parameter list: This is a list of one or more parameters that the function can take as input. Each parameter is a variable that will be assigned the value of the argument passed to the function when it is called.
4 The function body: This is the code that is executed when the function is called. It is contained within a pair of curly braces {} and may include any number of JavaScript statements, such as variable declarations, loops, conditional statements, and more.
Describe the parts of a function call.
- The function name: This is the name of the function that is being called.
- The argument list: This is a list of one or more arguments that are passed to the function when it is called. Each argument is a value that is assigned to the corresponding parameter of the function.
- The return value: This is the value that the function returns after it has finished executing its code.
When comparing them side-by-side, what are the differences between a function call and a function definition?
A function definition is a block of code that defines a function and specifies what it does. A function call is an expression that invokes a function and passes one or more arguments to it. Here are the main differences between a function call and a function definition in JavaScript:
- A function definition defines a function, whereas a function call invokes a function.
- A function definition specifies the code that will be executed when the function is called, whereas a function call causes the code in the function - - -body to be executed.
- A function definition includes the function keyword, the function name, the parameter list, and the function body, whereas a function call includes the function name and the argument list.
- A function definition is typically written only once, whereas a function call can be made any number of times.
- A function definition creates a function that can be called later on, whereas a function call is an expression that is evaluated at runtime.
What is the difference between a parameter and an argument?
a parameter is a variable that is declared in the function definition, and it is used to receive the value of the argument passed to the function when it is called. An argument is a value that is passed to a function when it is called, and it is assigned to the corresponding parameter in the function definition. In other words, a parameter is a placeholder for an argument that is passed to a function.
Why are function parameters useful?
they allow a function to take one or more inputs, which can then be used within the function body to perform a specific task. Allows us to re-run the function with new values.
What two effects does a return statement have on the behavior of a function?
- It causes the function to immediately stop executing and return control to the calling code.
- It specifies the value that the function returns to the calling code.
Here is an example of a return statement in JavaScript: