Created
August 26, 2016 14:58
-
-
Save esova-ana/02298c2a47d2a8d3778f061eac9ab45d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*--------------------------------------------------------------------------- | |
MORE ABOUT FUNCTIONS | |
----------------------------------------------------------------------------*/ | |
/*---------------------------------------- | |
1. WHEN TO USE RETURN | |
-----------------------------------------*/ | |
/*------------------------ | |
1.1. NO OUTPUT NEEDED | |
-------------------------*/ | |
There are so called pure functions and they have input and transform it into output with no side effects | |
The example: | |
function asdf (enemy) { | |
hero.attack(enemy) | |
} | |
is not a pure function ... it will take input but not give back output , because there is no return | |
Instead, the function "asdf", when called, will produce a side effect by calling hero.attack(...) which already creates the desired effect | |
If we would use the function "asdf" like this: | |
var enemy = hero.findNearestEnemy() | |
var x = asdf(enemy) | |
console.log(x) // => undefined | |
/*------------------------ | |
1.2.OUTPUT NEEDED | |
-------------------------*/ | |
function calculate (x) { | |
return x+2 | |
} | |
var y = calculate(5) | |
console.log(y) // => 7 (we use return, therefore we get output we need) | |
BUT if you do: | |
function calculate (x) { | |
x+2 | |
} | |
var y = calculate(5) | |
console.log(y) // => undefined (without using return, we don't get the output we need) | |
Sometimes functions do more, so you have to specify what you want to return | |
Example: | |
function calculate (x) { | |
var z = x + 2 | |
var q = z*2 | |
return q | |
} | |
var y = calculate(5) | |
console.log(y) // => 14 | |
/*---------------------------------------- | |
2. ARGUMENTS/PARAMETERS(PARAMS) | |
-----------------------------------------*/ | |
What is the task of arguments/params in functions | |
function(argument or parameter) { | |
//some code | |
} | |
// without functions... | |
var x = 5 | |
var result1 = (x*x + 2) -7 | |
hero.say(result1) // => 20 | |
var y = 7 | |
var result2 = (y*y + 2) - 7 | |
hero.say(result2) // => 42 | |
// with functions | |
function add2 (x) { return (x*x + 2) - 7 } | |
hero.say(add2(5)) // => 7 | |
hero.say(add2(7) // => 9 | |
The point of functions to write certain parts of your code only once and then repeat it by "delegating" to that code by using the functions name. | |
in the last to lines we call add2(5) and add2(7) | |
so in function add2 (x) ... x will be 5 the first time and 7 the next time ... | |
...which will execute the code inside the function definition { return (x*x + 2) - 7 } - the first time with x=5 and the next time with x=7 | |
Thus: | |
x in function add2 (x) { ... } is a placeholder called a PARAMETER | |
and when you call add2(5) you pass in 5 as the ARGUMENT | |
...later when you call add2(7) you pass in 7 as the ARGUMENT | |
Whenever you call a function, what you pass in as ARGUMENT(S) (e.g. 5 or 7) is the value of the PARAMETER(S) (e.g. x) when the code in the function definition is executed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment