Guest User

Untitled

a guest
Jun 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. function
  2.  
  3. * function needs to be defined first and then it can be called(executed)
  4. ```js
  5. // definition
  6. function add(a, b) {
  7. return a + b;
  8. }
  9.  
  10. // execution or you can say call function add with two parameters 1 and 2
  11. add(1, 2);
  12. ```
  13.  
  14. * function body
  15. All lines of code between `{` and `}` is called `function body`.
  16. ```js
  17. function add(a, b) {
  18. ... // more code
  19. ...
  20. ...
  21. ...
  22. ...
  23. return a + b;
  24. }
  25. ```
  26.  
  27.  
  28. * parameters and arguments
  29. ```js
  30. // a and b called arguments like placeholders
  31. // in this example, a holds value 1, b holds value 2
  32. // to access value 1, we use a - console.log(a) will get u 1
  33. // to access value 2, we use b - console.log(b) will get u 2
  34. function add(a, b) {
  35. return a + b;
  36. }
  37.  
  38. add(1, 2); // 1 and 2 called parameters - THE ACTUAL VALUES!!!
  39. ```
  40.  
  41. * return value
  42. Functions can have return values.
  43.  
  44. ```js
  45. function add(a, b) {
  46. return a + b; // return the value to outside
  47. }
  48.  
  49. var res = add(1, 2);
  50. console.log(res); // 3
  51. ```
  52.  
  53. Functions can have no return values sometimes.
  54.  
  55. ```js
  56. function add(a, b) {
  57. return; // return nothing!
  58. }
  59.  
  60. var res = add(1, 2);
  61. console.log(res); // undefined
  62. ```
  63.  
  64. By default, function returns `undefined`.
  65.  
  66. ```js
  67. function add(a, b) {
  68. a + b;
  69. }
  70.  
  71. var res = add(1, 2);
  72. console.log(res); // undefined because u don't return anything in function add
  73. ```
  74.  
  75. * Code after return statement
  76. Code after return statement will not be executed!
  77.  
  78. ```js
  79. function add(a, b) {
  80. var res = a + b;
  81. return res;
  82. // anything below will not run!!!!!!!!
  83. var b = 155;
  84. return b;
  85. }
  86.  
  87. var res = add(1, 2);
  88.  
  89. console.log(res); // 3 not 155
  90. ```
Add Comment
Please, Sign In to add comment