Functions Review
Storing function evaluations
Functions within functions
Intro to Scope
var add = function (a, b) {
return a + b;
};
add(1, 2);
declaration / definition
invocation / call time
< Note 'function' keyword
< Note invocation operator ()
var add = function (a, b) {
return a + b;
};
add(1, 2);
parameters
body
parameters
DRY: Do not repeat your code!
Expression: More expressive code!
User Input: We can create actions
without knowing the user input.
The value of a function is what is placed after its return statement.
var add = function(a, b){
return a + b;
};
add(4, 5); // => 9
Like anything else, we can assign that value to a variable.
var add = function(a, b){
return a + b;
};
var nine = add(4, 5);
console.log(nine); // => 9
Why would we do this?
var add = function(a, b){
return a + b;
};
var nine = add(4, 5);
console.log(nine); // => 9
Why save function evaluations?
var nine = add(4, 5);
var square = function(a){
return a * a;
};
var eightyOne = square(nine);
console.log(eightyOne); // => 81
Allows us to separate functionality.
// `add` will get us the number that we want.
var nine = add(4, 5);
var square = function(a){
return a * a;
};
// `square` will square what we want
var eightyOne = square(nine);
console.log(eightyOne); // => 81
More complex functionality...
var addAndSquare = function(a, b){
var sum = a + b;
var square = sum * sum;
return square;
};
// This technique is called `imperative`
// programming. We are building out each step bit
// by bit.
var addAndSquare = function(a, b){
var sum = add(a, b);
var squareSum = sum * sum;
return squareSum;
};
// We have replaced our a + b with
// our add function invoked with our
// parameters.
var addAndSquare = function(a, b){
var sum = add(a, b);
var squareSum = square(sum);
return squareSum;
};
// We have replaced sum * sum with
// our square function invoked with our
//sum variable.
Variables in the global scope can be accessed anywhere, and by anything in your function.
Variables in the local scope can only be accessed in that scope, or in 'child' scopes relative to that function.
Intro to Scope
var globalScope = true;
var addAndSquare = function(a, b){
var sum = add(a, b);
var squareSum = square(sum);
return square;
};
Scope is local to the `addAndSquare function`
Scope is global to the `addAndSquare` function
Scope is the parent scope to the
`addAndSquare` function
Scope is child scope to the global object.
Let's dive deeper into addAndSquare
var addAndSquare = function(a, b){
var sum = add(a, b)
var square = square(sum)
return square;
};
var eightyOne = addAndSquare(4, 5)
// inside of the local scope addAndSquare
// passing the arguments into `add` child scope
var sum = add(4, 5)
// inside local scope of `add` function
return 4 + 5
// inside of the local scope addAndSquare
var sum = add(4, 5)
// passing our sum into the square function
var square = square(sum)
return square;
// inside local scope of `square` function
return sum * sum
// back inside local scope of addAndSquare function
var sum = add(4, 5)
var square = square(sum)
// returning the square variable
return square;
// back in the global scope of the program
var addAndSquare = function(a, b){
var sum = add(a, b)
var square = square(sum)
return square;
};
// saving the returned results to the variable eightyOne
var eightyOne = addAndSquare(4, 5)
var addAndSquare = function(a, b){
var sum = add(a, b) // currently here
var square = square(sum)
return square;
};
var add = function(a, b){
return a + b // currently here
};
var addAndSquare = function(a, b){
var sum = add(a, b)
// currently here
var square = square(sum)
return square;
};
var square = function(num) {
return num * num // currently here
}
var addAndSquare = function(a, b){
var sum = add(a, b)
var square = square(sum)
// currently here
return square;
};
var eightyOne = addAndSquare(4, 5)
// currently here
var globalScope = true
var addAndSquare = function(a, b){
var sum = add(a, b)
var square = square(sum)
return square;
};
var eightyOne = addAndSquare(4, 5)
https://github.com/TelegraphPrep/06-2016-calculator