week 3
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
functions and an introduction to higher-order functions
Functions Review
Introduction to Higher Order Functions
Overview
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Functions
function declaration, function expression and Anonymous Functions
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
function declaration
function square (x) {
return x * x;
};
A Function Declaration defines a named function without requiring var keyword
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
function expression
var square = function (x) {
return x * x;
};
A Function Expression defines a function as a part of a larger expression syntax (typically a variable assignment )
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
function (num1, num2) {
return num1 + num2;
};
The function name. Can be omitted, in which case the function is anonymous.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
(function (num1, num2) {
return num1 + num2;
})();
Invocation: wrap your function in an IIFE (immediately invoked function EXPRESSION)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
(function (num1, num2) {
return num1 + num2;
})();
Usage: anonymous functions can only be called once
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
(function (num1, num2) {
return num1 + num2;
})();
Would this ever be helpful?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Definition:
A function that takes another function
in as a PARAMETER
The function passed in as a parameter is called a CALLBACK
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
each
//loops through an array
//and invokes a callback on each element
function each (arr, callback){
for(var i = 0: i < arr.length; i++){
callback(arr[i]);
}
};
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Example: each
function each (arr, callback){
for (var i = 0; i < arr.length; i++){
callback(arr[i]);
}
};
// prints an element to the console
var print = function(element){
console.log(element);
};
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Identifying the Callback
var arr = ['hi', 'my', 'name', 'is', 'albrey'];
each(arr, print);
// hi
// my
// name
// is
// albrey
callback!
higher order function
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Converting to an Anonymous Function
var arr = ['hi', 'my', 'name', 'is', 'albrey'];
each(arr, function(element) {
console.log(element);
});
// hi
// my
// name
// is
// albrey
callback!
higher order function
anonymous functions, intro to HOF and testing
By telegraphprep
anonymous functions, intro to HOF and testing
An introduction to functional programming
- 815