Skip to content

Instantly share code, notes, and snippets.

@Ncreshon
Last active June 12, 2017 05:22
Show Gist options
  • Select an option

  • Save Ncreshon/f9532dbce8299ebac486933579669749 to your computer and use it in GitHub Desktop.

Select an option

Save Ncreshon/f9532dbce8299ebac486933579669749 to your computer and use it in GitHub Desktop.
JS Bin[Functions: Programs within progrms]// source https://jsbin.com/qotidus
//Functions//
/* Functions are like instructions that can be stored
* to use over and over again. They can be named, annonymous
* or assigned to a varible or constant. */
/* There are two phases of using functions.
* 1. Declaring the function-- creating it
* 2. calling or invocation of the function to use it */
//Named function syntax (use keyword function)//
/* function name(parameter1, parameter2) {
* function body (code to be executed)
* }
* Named functions can be used by the program before they
* appear before it is sequentially appears because it is hoisted
* to the top of their scope.
*
* Parmaeters are placeholders for the required input that
* needs to be passed into the function. You can set as many parameters
* needed for your function. The value being passed is called
* the arguement.
* To return a value use keyword return*/
function example(para1, para2) {
return para1 + para2;
} //defined the function//
console.log(example(2,18)); //called the function. will return 20//
//Function Expression//
/* a fucntion is when an anonymous function is assigned
* to a varible or const */
var whatIf = function(num1,num2){
if(num1 > num2) {
return num1 + " is greater than";
} else if(num1 < num2){
return num2 + " is greater than";
}else {
return "Idk";
}
};
console.log(whatIf(3,5)); // prints "5 is greater than"//
//Closures//
/* A closure is an inner function that has access to the outer function's
* variable scope chain.The closure has access to 3 scope chains. It can access
* it's own scope, it has access to the outer function's variables, and the global
* variables. */
function examples(artist) {
var name = "My favorite artist is ";
function examplesAgain() {
return name + artist;
}
return examplesAgain();
}
console.log(examples("Lloyd")); //prints "My favorite artist is LLoyd"//
// NOTE: Primitive (simple) values are passed to a function BY COPY, complex by reference. //
function tryingIt(string,string2) {
return string + " " + string2;
}
console.log(tryingIt("I love R&b.", examples())); // "I love R&b. My favorite artist is Lloyd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment