Home  >  Article  >  What is the difference between pass by value and pass by address?

What is the difference between pass by value and pass by address?

coldplay.xixi
coldplay.xixiOriginal
2021-02-25 11:32:2015037browse

The difference between passing by value and passing by address: 1. In passing by value, the function parameter is pushed onto the stack, and any modification is applied to the copy and not to the original variable; 2. Passing by value What is pushed onto the stack at the address is a copy of the pointer variable. When you perform a pointer operation on the pointer, its value points to the original variable.

What is the difference between pass by value and pass by address?

#The operating environment of this article: Windows 7 system, Dell G3 computer.

The difference between passing by value and passing by address:

1. In passing by value, the function parameters are pushed onto the stack, and any modifications are made on the copies. , has no effect on the original variables.

2. What is pushed onto the stack during address transfer is a copy of the pointer variable. When you operate on the pointer, its value points to the original variable, so the original variable is operated.

What is the difference between pass by value and pass by address?

Extended information

There are three ways to pass parameters: passing by value, passing by address, and passing by reference.

1. Pass by value

(1) Formal parameters and actual parameters each occupy an independent storage space.

(2) The storage space of formal parameters is allocated when the function is called. When the call starts, the system opens a temporary storage area for the formal parameters, and then passes each actual parameter to the formal parameter. This is the formal parameter. The value of the actual parameter is obtained.

2. Address transfer

The difference between address transfer and value transfer is that it transfers the storage address of the actual parameter to the formal parameter, so that the formal parameter pointer and the actual parameter pointer point to the same address. Therefore, any changes in the address pointed to by the formal parameter pointer in the called function will affect the actual parameters.

3. Passing by reference

Passing by reference takes a reference as a parameter, which not only allows any operation on the formal parameters to change the corresponding data, but also makes function calls convenient. Passing by reference is to add the reference operator "&" before the formal parameter call.

The reference is an alias of an actual parameter. If it is the same variable as the actual parameter, their values ​​will be the same. If the reference changes, its actual parameter will also change.

Related free recommendations: Programming video course

The above is the detailed content of What is the difference between pass by value and pass by address?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn