Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="What are function?">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>Functions</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. /* Functions
  13. Functions are blocks of code designed to perform a task. A function defenition
  14. consists of a function name, parameters to the function, and statements that define
  15. the function.
  16. */
  17.  
  18. // 1. Declaration and definition
  19. function myName(myParameter) { // my function name and parameters in (). This is how we input data.
  20. return myStatement = myParameter; // my statement
  21. }
  22. console.log(myName("argument")); // calling the function and argument for our parameter
  23.  
  24. // The parameter in our function is simply a placeholder for our value passed through our argument
  25. // when we call the function.
  26.  
  27. // 2. We can also assign function to variables
  28.  
  29. var myFunc = function(str, str2){return str + " " + str2}; // we use return when we want a value out of our function
  30. console.log(myFunc("Hello", "World"));
  31.  
  32. // Function scope and Closure
  33. // In the example below we show we can access globally scoped variables in our function
  34.  
  35. function randomNum() {
  36. var myNum = Math.floor(Math.random()*10); // This will give me a random number value between 1 and 10
  37. return myNum; //The variable is in our local scope
  38. }
  39. var random = randomNum(); //Value of function stored globaly
  40.  
  41. function test(ran){
  42. newNum = ran + 5;
  43. return newNum;
  44. }
  45.  
  46. console.log(random);
  47. console.log(test(random)); //invoking our stored function value to the test function
  48. console.log(newNum);
  49. </script>
  50.  
  51.  
  52.  
  53. <script id="jsbin-source-javascript" type="text/javascript">/* Functions
  54. Functions are blocks of code designed to perform a task. A function defenition
  55. consists of a function name, parameters to the function, and statements that define
  56. the function.
  57. */
  58.  
  59. // 1. Declaration and definition
  60. function myName(myParameter) { // my function name and parameters in (). This is how we input data.
  61. return myStatement = myParameter; // my statement
  62. }
  63. console.log(myName("argument")); // calling the function and argument for our parameter
  64.  
  65. // The parameter in our function is simply a placeholder for our value passed through our argument
  66. // when we call the function.
  67.  
  68. // 2. We can also assign function to variables
  69.  
  70. var myFunc = function(str, str2){return str + " " + str2}; // we use return when we want a value out of our function
  71. console.log(myFunc("Hello", "World"));
  72.  
  73. // Function scope and Closure
  74. // In the example below we show we can access globally scoped variables in our function
  75.  
  76. function randomNum() {
  77. var myNum = Math.floor(Math.random()*10); // This will give me a random number value between 1 and 10
  78. return myNum; //The variable is in our local scope
  79. }
  80. var random = randomNum(); //Value of function stored globaly
  81.  
  82. function test(ran){
  83. newNum = ran + 5;
  84. return newNum;
  85. }
  86.  
  87. console.log(random);
  88. console.log(test(random)); //invoking our stored function value to the test function
  89. console.log(newNum);
  90.  
  91.  
  92.  
  93. </script></body>
  94. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement