Functions Flashcards
1
Q
functions
A
- allows us to reuse blocks of code.
- Allows is to wrap blocks of code into REUSABLE packages.
- You call a function to execute the function. Similarly you run a program to execute its code.
- mini programs inside your program
- The main point of functions is to get rid of duplicate code
3
Q
Argument
A
- Value passed in the function call.
- The input to functions are arguments
- The output is the return value
4
Q
Parameter
A
• Variable(s) in between the function’s parentheses in the definition statement.
5
Q
return
A
- sends back a value
- can’t pass functions to variables without it.
- Stops the execution of a function. This feature of “return” is similar in operation to “break”
- Every function returns something, but if “return” is not explicitly used it always returns “undefined” after the output is printed
6
Q
Function Declaration vs. Function Expression
A
//function declaration function test1() { return xyz; } //function expression var test1 = function() { return xyz; } • two ways to declare a function • if the latter is used if you pass a different value the function is lost
7
Q
Scope
A
- Context of what some code can be executed in
- if you define a variable inside a function it can only be used inside the function. This is also known as local variable.
- Variables that was declared outside the function can be accessed inside the function.
8
Q
Higher Order functions
A
• Any function that takes a function as an argument, returns a function or both.
setInterval(sing, 1000); when you pass "sing" this way without () you passing the code so that setInterval will execute it. • setInterval(function(){ //statement; }); - used when you want to pass a function but don't know what it is yet.
9
Q
Call
A
run or execute a function