Skip to content

Instantly share code, notes, and snippets.

@gonewayword
Forked from anonymous/index.html
Created August 29, 2016 00:42
Show Gist options
  • Save gonewayword/933758fd107d395ab241c0a538e0e674 to your computer and use it in GitHub Desktop.
Save gonewayword/933758fd107d395ab241c0a538e0e674 to your computer and use it in GitHub Desktop.
Describing functions as best one can functionFunctions make a program go round// source http://jsbin.com/siwivo
//Functions: programs within programs!
//Functions are the internal combustion if variables are the fuel. Functions are the trebuchets, if variables are the boulders.
//Enough metaphors. Functions are the action in a program that keeps the program moving along, they are what gets executed.
//The two necessary phases of a function are declaring it, and executing it--also calling a function or invoking a function.
//To declare a function there are a couple ways.
var scrumtrelescent = function(para1, para2) {}
function scrumtrelescence(para1, para2) {}
var scrumArray = [12, "Noggins", function(para1, para2){}]
//There are many specific variations for declaring a function. The first two above are the most common. The first declaration is not hoisted,
//meaning you cannot call it before you have declared it. The second version is hoisted--if you called the function beforehand the interpreter
//would still know what you're referring to and would invoke the function. The third is an array including an anonymous function, and the
//reason it's in an array is because an anonymous function has no name, hence you cannot call it in an ordinary
//way. As is the case here, the function would need to exist in relation to something else, and you could call the third item [spot 2] in the
//array to call that function. The only way to call an anonymous function without putting it in relation to something else is
//to declare it as a self-invoking function.
//To execute/call/invoke a function, you state the name of the function and then a pair of parenthesis, like so:
// scrumtrelescent();
//It will still invoke a function if you call it as I did above, without values for the parameter spaces, and if the function has simple straightforward commands, like
//say return something no matter what, then it will happen when I call it. Of course, if the values/parameters come to play within the function and
//I do not declare any values as above, then the aspect of the function using those parameters will not work.
// scrumtrelescent("James Lipton", 100);
//The above is the function, invoked and including two values for the two parameter slots.
//A function can totally exist without parameters. But parameters certainly add layers of capability to a function. The parameters are
//entered into the parenthesis after the function name when the function is declared. They are really just variable names, and can be called
//nearly anything you want--the parameters themselves have no value to them. When declared, they are simply waiting for value to be given to them,
//like a couple of adorable eager parameter doges waiting for a task to complete. As shown above, no value necessarily need be given to them for
//the function to be passed.
//When a value is passed into the parameters, by entering said value into the corresponding position of the parameters, it's called passing an
//argument. Thought about another way, the function is the set of rules and/or commands that will be followed when that function is invoked.
//The parameters are, essentially, function variants, the parts that change in order for the function's results to change.
//Let's say the function is the structure of your car. The parameters are buttons waiting to be pressed. And when you turn your wheel to the right,
//you're passing the right turn argument, causing your car to go right. Likewise for left. The car is all built and ready to go; you just need to
//tell it where to go.
//NAMED FUNCTION:
//A named function is declared like so:
function geriatric(){};
//FUNCTION ASSIGNED TO VARIABLE
var geriatric2 = function() {};
//Functions can OPTIONALLY take inputs and OPTIONALLY return a single value, how do we specify inputs, and how do we return a value?
var scrumtrelescent = function(para1, para2) {
return para1 + para2;
}
scrumtrelescent(12, 84);
//Scope can be global or local. Functions can use/modify variables that exist in the parent or global scope, but variables named inside of a
//function are local and thus cannot by seen or used by other functions outside of it. For example:
var scrumtrelescent = function(para1, para2) {
return para1 + para2;
var willFerrell = "funny"
}
scrumtrelescent(12, 84);
console.log(willFerrell); //this will return as undefined because the variable was declared inside a function and therefore is not global.
//If you name a variable without the prefix "var" before it, this variable is global no matter where you named it. E.g.:
globalVariableNoMatterWhat = "Placebo Effect";
//Closures: Functions form closures around the data they house. If an object returned from the Function and is held in
//memory somewhere (referenced), that closure stays ALIVE, and data can continue to exist in these closures!
//(See: our meeting-room app for an example!) (ALSO, see: Understanding JavaScript Closures with Ease)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment