javascript-functions Flashcards

1
Q

What is a function in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the parts of a function definition.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe the parts of a function call.

A
  1. The function name: This is the name of the function that is being called.
  2. 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.
  3. The return value: This is the value that the function returns after it has finished executing its code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When comparing them side-by-side, what are the differences between a function call and a function definition?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between a parameter and an argument?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why are function parameters useful?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What two effects does a return statement have on the behavior of a function?

A
  1. It causes the function to immediately stop executing and return control to the calling code.
  2. It specifies the value that the function returns to the calling code.
    Here is an example of a return statement in JavaScript:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly