Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function
- * function needs to be defined first and then it can be called(executed)
- ```js
- // definition
- function add(a, b) {
- return a + b;
- }
- // execution or you can say call function add with two parameters 1 and 2
- add(1, 2);
- ```
- * function body
- All lines of code between `{` and `}` is called `function body`.
- ```js
- function add(a, b) {
- ... // more code
- ...
- ...
- ...
- ...
- return a + b;
- }
- ```
- * parameters and arguments
- ```js
- // a and b called arguments like placeholders
- // in this example, a holds value 1, b holds value 2
- // to access value 1, we use a - console.log(a) will get u 1
- // to access value 2, we use b - console.log(b) will get u 2
- function add(a, b) {
- return a + b;
- }
- add(1, 2); // 1 and 2 called parameters - THE ACTUAL VALUES!!!
- ```
- * return value
- Functions can have return values.
- ```js
- function add(a, b) {
- return a + b; // return the value to outside
- }
- var res = add(1, 2);
- console.log(res); // 3
- ```
- Functions can have no return values sometimes.
- ```js
- function add(a, b) {
- return; // return nothing!
- }
- var res = add(1, 2);
- console.log(res); // undefined
- ```
- By default, function returns `undefined`.
- ```js
- function add(a, b) {
- a + b;
- }
- var res = add(1, 2);
- console.log(res); // undefined because u don't return anything in function add
- ```
- * Code after return statement
- Code after return statement will not be executed!
- ```js
- function add(a, b) {
- var res = a + b;
- return res;
- // anything below will not run!!!!!!!!
- var b = 155;
- return b;
- }
- var res = add(1, 2);
- console.log(res); // 3 not 155
- ```
Add Comment
Please, Sign In to add comment