Functions Flashcards

1
Q

What is a function?

A

reusable block of code that groups together a sequence of statements to perform a specific task

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

What is function declarations?

A

binds a function to a name (identifier)

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

What is Hoisting?

A

allows access to function declarations before they’re defined.

Ex)

greetWorld(); // Output: Hello, World!

function greetWorld() {
 console.log('Hello, World!');
}

//Note: hoisting isn’t considered good practice, we simply want you to be aware of this feature.

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

What does it mean to ‘call a function’?

A

Code in the function block { } is executed only when the function is called.

No limits to times functions can be called

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

What are parameters?

A

Parameters allow functions to accept input(s) and perform a task using the input(s).

We use parameters as placeholders for information that will be passed to the function when it is called.

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

What is an Argument? When does it occur?

A

When calling a function that has parameters, we specify the values in the parentheses that follow the function name.

The values that are passed to the function when it is called are called arguments. Arguments can be passed to the function as values or variables.

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

Why is order of arguments important?

A

order in which arguments are passed and assigned follows the order that the parameters are declared.

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

Create a function with one argument that prints out:

Thank you for your purchase Cole! We appreciate your business.

Also include how you would call the function with the ‘cole’

A

function sayThanks(name) {

console.log(‘Thank you for your purchase ‘+ name +’! We appreciate your business.’);

}

sayThanks(‘Cole’);

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

What do default parameters do?

A

Allow parameters to have a predetermined value in case there is no argument passed into the function or if the argument is undefined when called.

Ex)

function greeting (name = 'stranger') {
 console.log(`Hello, ${name}!`)
}

greeting(‘Nick’) // Output: Hello, Nick!
greeting() // Output: Hello, stranger!

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

What does the keyword return do?

A

We can use the keyword return to capture the output of a function to a variable.

By default that resulting value is undefined.\

Ex)

function rectangleArea(width, height) {
 if (width \< 0 || height \< 0) {
 return 'You need positive integers to calculate area!';
 }
 return width \* height;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will print out:

function monitorCount(rows,columns){

return rows*columns;

}

const numOfMonitors=monitorCount(5,4)

console.log(numOfMonitors)

A

20

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

What are helper functions?

A

function that performs part of the computation of another function.

Helper functions are used to make your programs easier to read by giving descriptive names to computations. They also let you reuse computations, just as with functions in general.

Ex)

function monitorCount(rows, columns) {

return rows * columns;

}

//function saves the product of two arguments to monitorCount(arg1,arg2)

function costOfMonitors(rows,columns){

return monitorCount(rows,columns)*200;

}

//function saves product of called monitorCount(row,columns) /*note: see line 2*/ times 200

const totalCost=costOfMonitors(5,4)

//we save a called function of costOfMonitors with 2 arg that trigger previous programmed functions, all saved to object called totalCost.

console.log(totalCost)

//prints output value of functions programmed in object: totalCost

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

What is a function expression?

What is a function with no name ?

A

define a function inside an expression, we can use the function keyword.

An anonomus function

Ex)

const plantNeedsWater= function(day) {

if (day===”Wednesday”) {

return true;

}

else {

return false;

}

};

console.log(plantNeedsWater(‘Tuesday’))

Note: A function expression is often stored in a variable in order to refer to it.

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

What is an arrow function?

A

Quick way to write a function using =>

Ex)

const rectangleArea = (width, height) =\> {
 let area = width \* height;
 return area;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is consice body?

A

The most condensed form of the function

1) Functions that take only a single parameter do not need that parameter to be enclosed in parentheses. However, if a function takes zero or multiple parameters, parentheses are required.
2) A function body composed of a single-line block does not need curly braces. Without the curly braces, whatever that line evaluates will be automatically returned. The contents of the block should immediately follow the arrow => and the return keyword can be removed. This is referred to as implicit return.

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

What is implicit return?

A

The contents of the block should immediately follow the arrow =>

Implied return

Ex)

const squareNum = (num) =\> {
 return num \* num;
};

Same as:

const squareNum = num => num * num;

17
Q

For following, rewrite using concise body:

const plantNeedsWater = (day) => {

return day === ‘Wednesday’ ? true : false;

};

A

const plantNeedsWater = day => day===’Wednesday’ ? true : false;