FUNCTIONS Flashcards

1
Q

What is a function declaration?

A

A Function declaration is just an inactive statement containing code (set of instructions).

A function declaration is a statement that creates an inactive function with the specified name and parameters. To declare a function simply means: create a function statement, without executing it.

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

What is a function?

A

A function is a reusable block of code.

Functions are one of the fundamental building blocks in JavaScript. A function is a block of code containing statements that perform a particular task or return some value.

Parts of the program can be wrapped up with functions and then easily reused in multiple places in our program.

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

What is the syntax for a function?

A

function name ( /* parameters / ) {
/
the code to execute
when the function is called */
}

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

What are the 3 steps for creating a function ?

A
  • First, we declare a function named makePastaAlfredo: function makePastaAlfredo () {}

Second, we add code to the function’s body - the instructions Select the ingredients, Mix the ingredients and Serve.

Last, we call the function by adding the () after the function name: makePastaAlfredo() . Adding () after the function name will run the code in the function. We repeat the function call 3 times, which will run the function and its code 3 times.

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

What are the 4 key parts of a function?

A

A function declaration consists of 4 key parts:

  • the keyword function - a function declaration must start with the keyword function.
    *the function’s name - function name is the identifier by which the function can be called (accessed).
  • parameters - a list specifying the input values that the function is expecting.
  • the code to execute - a set of instructions that will be executed when the function runs.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Invoking a function

A

The function invocation is the process of executing (calling) a function. To invoke a function, add a set of parenthesis () after the function’s name.

Just because we created a function, doesn’t mean it will be used. To run the function and make it do something, we need to invoke (call) it.

The function will run the code within the curly brackets {} as many times as you call it. If you invoke the function 3 times, it will run it 3 times.

example : functionName()

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

What is the parameters ?

A

A parameter - is a placeholder for the value the function is expecting. Parameters are set during the function declaration.

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

What is the arguments?

A

When invoking a function, we can optionally pass it some input, also known as arguments.

To be able to pass input to the function, we first have to list the values (parameters) that the function is expecting.

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

What happens to additional arguments ?

A

In JavaScript, additional arguments (not specified as function’s parameters) passed during the function’s invocation get ignored.

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

What would happen if we invoke a function but fail to provide all the arguments?

A

If we fail to provide an argument that the function is expecting, its value will be set to undefined.

Functions parameters serve as placeholders for values that will be provided during the invocation.

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

What are parameters exactly ?

A

Parameters are variables that will store the argument’s value, once the function is invoked. If the value is not provided during the invocation, the parameter variable will be set to undefined.


Function **parameters are **placeholder variables that store argument values provided when the function is invoked.

If an expected argument is not provided during the invocation, the value is set to undefined.

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

What does return keyword ?

A

Returning Values
The return keyword stops the function’s execution and returns the provided value.

The value returned from the function becomes available in the place where the function was invoked.

Functions won’t always be going to print something in the console. Sometimes you want to do some action and return a value back.

To return a value from a function call, use the keyword return in your function.

A function can only return one value, but the value can be of any type: string, number, boolean, object, array

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

What is the difference between return & console.log()?

A

Important: The return keyword and console.log() are two different things:

The console.log - is only used to print content to the console where we can see it.
The keyword return - is used to return a value from a function.
When working with functions, always use the return keyword to return a value from the function.

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

What is important to remember about the return keyword?

A

One important thing to keep in mind is that the return keyword ends the function’s execution.

Important: The return ends the function immediately. The remaining code in the function after the return won’t be executed.

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

What’s going on if your forgetting the return keyword?

A

When writing functions, a typical mistake is to forget the return keyword. If the return keyword is omitted, the function will by default return undefined

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

Convention naming functions?

A
  • Function names should always begin with a lowercase letter
  • Always name your functions using the camelCase notation
  • Always name your functions starting with a verb that describes its actions, like doSomething
  • Do not use special characters like - or _ when naming your functions