What are formal parameters and actual parameters? How to use it in Javascript?

In the code inside the function, when some values ​​cannot be determined, it can be received from the outside through the parameters of the function. A function can complete different operations by passing in different parameters.

Function parameters are divided into formal parameters and actual parameters. When declaring a function, you can add some parameters in parentheses after the function name, these parameters are called formal parameters. When the function is called, it also needs to pass the corresponding parameters, which are called actual parameters. The formal parameters of the function are formal parameters, because when the Li number is declared, the function has not been called, and it is uncertain what values ​​these parameters will pass. The actual parameter is the actual parameter, and its value is determined when the function is called.

The specific grammatical forms of function parameters and actual parameters are as follows.

function 函数名(形参1,形参2,...)     //函数声明的小括号里的是形参
  //函数体代码
}
函数名(实参1,实参2,...);            //函数调用的小括号里的是实参

A function can have multiple parameters, separated by commas, or no parameters. Below we demonstrate the specific use of function parameters through code.

function cook(arg)  {
    
    
  console.log(arg);
}
cook('potato');

In the above code, arg is the formal parameter of the function, which is similar to a variable. When the function is called, its value is the value passed in when the call is made, namely potato.

Next, we will demonstrate how to use the function to find the sum of any two numbers. The specific code is as follows.

function getSum(numl,num2) {
    
    
  console.log(numl + num2);
}
getSum(1,3);  
//输出结果:4
getSum(3,8);
   //输出结果:11

In the above code, the code on line 4 passes in two actual parameters when calling the function, which are 1 and 3 respectively, and these two actual parameters correspond to the formal parameters num1 and num2 in the function, and then modify this in line 2 The two values ​​are added, so the resulting output is 4. Similarly, the code on line 5 passed two actual parameters of 3 and 8 when calling the function, so the result is 11.

Learn a trick:

The formal parameter of a function can be regarded as a variable. When we pass a value type variable as a parameter to a formal parameter of a function, we actually copy the value of the variable in the stack space to the formal parameter. Then inside the method Any modification to the formal parameters will not affect the external variables.

function fn(a) {
    
    
    a++;
    console.log(a); 
}
var x = 10;
fn(x);
console.log(x)

When we pass a reference type variable to a formal parameter, we actually copy the heap address saved by the variable in the stack space to the formal parameter. The formal parameter and the actual parameter actually store the same heap address, so the operation is the same object.

function Person(name) {
    
    
    this.name = name;
}
function f1(x) {
    
     // x = p
    console.log(x.name); // 2. 这个输出什么 ?    
    x.name = "张学友";
    console.log(x.name); // 3. 这个输出什么 ?    
}
var p = new Person("刘德华");
console.log(p.name);    // 1. 这个输出什么 ?   
f1(p);
console.log(p.name);    // 4. 这个输出什么 ?

Guess you like

Origin blog.csdn.net/cz_00001/article/details/131352432
Recommended