How to use functions in Java script

How to use functions in Java script

Preface

What is a function is to encapsulate a relatively independent code block with a specific function to form an independent entity, which is a function. Give it a name (function name), which can be called repeatedly during development. The function of a function is to encapsulate a piece of code. Can be reused.

1. Function declaration and call

1.1 Statement

  • Keyword declaration
function function name(){
 //Function body
}
  • Expression declaration
var fn = function() {
 //Function body
}
  • Features:

When a function is declared, the body of the function is not executed, only when the function is called.

A specific function is generally used to doing one thing

1.2 Call

  • The syntax of calling a function:
Function name();
  • Features:

The function body will only be executed when it is called, and the call needs () to be called.

Can be called multiple times (repeated use)

//declare function
function sayHi() {
  console.log("Have you eaten yet?");
}

//Call functions
sayHi();

//Find the sum of all numbers between 1-100
function getSum() {
  var sum = 0;
  for (var i = 0; i <100; i++) {
    sum += i;
  }
  console.log(sum);
}
//A piece of code can be called multiple times
getSum();
getSum();
getSum();

1.2 Parameters

  • Why have parameters
function getSum() {
  var sum = 0;
  for (var i = 1; i <= 100; i++) {
    sum += i;
  }
  console.log();
}

//Although the above code can be called repeatedly, it can only calculate the value between 1-100
//What if you want to calculate the sum of all the numbers between nm?
  • syntax:
//Inside the function is a closed environment, you can pass external values ​​to the inside of the function through parameters
//Function declaration with parameters
function function name (parameter 1, formal parameter 2, formal parameter...) {
 //Function body
}

//Function call with parameters
Function name (actual parameter 1, actual parameter 2, actual parameter 3);
  • * Formal and actual parameters *

Formal parameters: When declaring a function, in order to be more flexible in the function of the function, some values ​​are not fixed, for these unfixable values. We can set parameters to the function. This parameter does not have a specific value, but only plays a role in occupying a position. We usually call it a formal parameter, also called a formal parameter. Actual parameters: If the function is declared with formal parameters, then the corresponding parameters need to be passed in when the function is called. We call the passed-in parameters the actual parameters, or actual parameters.

function fn(a, b) {
  console.log(a + b);
}
var x = 5, y = 6;
fn(x,y); 
//x, y actual parameters have specific values.
//When the function is executed, a copy of x and y will be copied to a and b inside the function,
//The value inside the function is the new value copied, and the external x, y cannot be modified

When JS function is called, it is allowed to pass multiple actual parameters, that is, the number of actual parameters can be more than the number of formal parameters;

1.3 The return value of the function

When the function is executed, the result is not always printed. We expect the function to give me some feedback (for example, the result of the calculation is returned for subsequent operations). At this time, the function can return something. That is, the return value. The function returns a value through return

Return value syntax:

//Declare a function with a return value
function function name (parameter 1, formal parameter 2, formal parameter...) {
 //Function body
  return return value;
}

//You can receive this return value through a variable
var variable = function name (actual parameter 1, actual parameter 2, actual parameter 3);

Detailed return value:

If the function does not explicitly use the return statement, then the function has a default return value: undefined
If the function uses the return statement, then the value following return becomes the return value of the function
If the function uses the return statement, but there is no value after return, then the return value of the function is also: undefined
After the function uses the return statement, the function will stop and exit immediately after the return statement is executed, which means that all other code after the return will not be executed again.

operation:

  • Find the sum of all numbers between 1-n
  • Find the sum of all numbers between nm
  • Find the maximum of 2 numbers

1.4 Other things related to functions

1.4.1 Anonymous function and self-calling function

Anonymous functions: functions without names

How to use anonymous functions:

Assign an anonymous function to a variable so that it can be called through the variable

If the anonymous function does not have any variable to represent it, then it cannot be directly called to execute, so it can be executed by the self-invoking method of the anonymous function

(function () {
  alrt(123);
})();

Regarding the role of self-executing functions (anonymous functions self-calling): prevent global variable pollution.

1.4.2 Functions themselves are also values

function fn() {}
console.log(typeof fn);
  • Function as parameter

Because a function is also a value type, you can use the function as a parameter of another function and call it in another function

function f1(f){
   //f = function f2(){
   //console.log(2);
   //}
    f();
}

function f2(){
    console.log(2);
}
f1(f2);//2
  • Function as return value

Because the function is a type, the function can be returned from the function as the return value.

function fn(b) {
  var a = 10;
  return function () {
    alrt(a+b);
  }
}

var f = fn(5);
f();//15
Reference: https://cloud.tencent.com/developer/article/1550589 How to use functions in Java script-Cloud + Community-Tencent Cloud