Passing of parameters

Source: Internet
Author: User

We should all know that the so-called programming language is through a series of function transformation, the realization of data transmission, transformation, and ultimately show the effect we need. It can be said that no matter what programming language data transmission is invaluable. However, we can not only transmit data in the process of simple transmission, we need a carrier to undertake our transmission of content, that is, I want to say the parameters.

Parameter physical parameters and arguments, so-called arguments can be understood as the actual parameters, is what we want to pass through the function of the content, and formal parameters? Simply speaking, the formal parameter is a bridge that uses it to receive the content that we pass over and deposit it into the storage unit, and then release the parameter. So how are the arguments and parameters passed? What changes have taken place in the course of their transmission? To follow the rules?

(1). The transfer of parameters is divided into 2 types:

1. Value passing

As the name implies, value passing is to pass the value to the parameter, that is, we just need to take out a formal parameter to accept the value is good, when I door in the function to modify the parameters, such as the name of the formal parameter, it will not affect the value of the actual parameter.

It is easy to see from the example that, although the contents of the formal parameters have changed, the results have not changed.

2. Reference delivery

Unlike value passing, a reference is passed to the address of the content. When using reference passing, the computer first finds the address of the argument, and then passes the address to the formal parameter, followed by the address of the data in turn into the parameters opened up space, so that once we modify the parameters inside the function, the computer can not find the corresponding address space, So the output is not the data we need.

(2). Number of parameters and arguments:

1. The arguments that are passed by the calling function must correspond to the number of formal parameters that are defined; (no such requirement in JavaScript)

2. Arguments beyond the shape parameters are not passed;

3. If no corresponding argument is passed in, the type of the formal parameter is defined as undefined;

All programming languages need to meet these 3 feature points, but in JavaScript, the number of arguments and parameters can not correspond.

(3). Functions that are primarily applied when parameters are passed

Arguments function: In the body of a function, the identifier arguments is a reference to an argument object, and once we pass the argument to the parameter, we create a arguments object; stored in the memory space of the image named arguments, note that arguments is never an array, he is an object.

Arguments applications: 1. Although he is not an array, we can still use it as an array to understand, through arguments we can get an element inside, or get the number of arguments passed in.

2. For arguments beyond the number of formal parameters, arguments also receives out-of-bounds arguments and stores them, which makes it easy for us to invoke the elements. Use function Arguments[i] to invoke;

The 3.arguments.length function is used to calculate the number of arguments passed;

4.argumrnts each function will have, therefore, arguments will only find its own internal arguments, cannot refer to the outer arguments.

Parameters of the basic content is also these, as long as the understanding of his transmission mechanism, we write the program will not feel helpless, seriously read this article, I think for the parameters of the transfer mechanism, you will have a clearer understanding, can not say let you taichetaiwu, but there are always some gains.

Passing of parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.