C language function parameter transfer principle, function parameter transfer

Source: Internet
Author: User

C language function parameter transfer principle, function parameter transfer

There are two methods for passing parameters in C language: stack-based and register-based. This time, we will only describe the first parameter transfer method in detail. The other method is not described here.

First, let's take a look at the following simple call routine:

Int Add (int a, int B, int c)

{

Return a + B + c;

}


Void main ()

{

Int x = 0, y = 1, z = 2;

Int result = 0;

Result = Add (x, y, z );

Printf ("Result for x + y + z % d", result );

}

With the above examples, let's take a look at how the parameters are passed. First, we understand a knowledge point, that is, the "()" operator has many meanings in C language, such as combiner operators and function call operators. The result = Add (x, y, z) Statement is of course a function call character. Therefore, in the compilation stage, the execution sequence after the function call character is, starting from the last parameter, the stack is pressed in the order of z-> y-> x until all the parameters are pressed to the call stack, then call the function address corresponding to Add to start execution. After the stack is pressed, the stack space is in the order of x-> y-> z from top to bottom (because the stack growth direction is upward, that is, the address is low, therefore, x is at the lowest address, that is, the top of the stack ). Therefore, parameter reading and operations in the called function also occur in the stack.


Whether the C language function parameters pass the value or copy

1 parameter
1. All parameters are transferred as copies of values. (If you want to know why, go to the parameter pressure stack of function calls that learn compilation principles and the corresponding content of the output stack ).
2 C passed the pointer in, in fact, this pointer value is also transferred to the copy. However, because the pointer value points to an external memory space (in fact, it is more of a heap space or an outer stack variable space), it is possible to change the external variables in the function. In essence, it is still transferred by copy, but it is a channel for accessing variables.
Therefore, if we want the function to change the external pointer value, we usually include the pointer of the pointer variable. Haha, many C beginners are very difficult to understand.

Binary Return Value
The return value is transmitted by copy. A value is generated after the function is released to the stack. The value remains valid throughout the lifecycle of the code segment that calls the function. It is equivalent to the call point to form an anonymous stack variable.

Variable a = function (); and a is not the value of return in the function.
In fact, the execution result of function () itself is an anonymous variable. (In fact, the compiler will check the syntax, such as the above a = function syntax, anonymous variables will not be generated, directly use a variable to copy the return value)
For example, function () returns the int value. Int x = function () + 6; // Note: When the + operation is performed, the function has been executed and all function stack operations have been completed.
Obviously, the function () must have a variable or constant involved in the calculation, and the return value in the function will be deleted as the function call ends out of the stack, so it must be copied and transmitted.

What is passed by C function call parameters?

There are two types: pass by value and pass by address,
Passing by value means creating a temporary memory space in the subfunction and saving the passed value in this space. When the subfunction ends, the memory space will be released!
Address-based transmission transfers the first address of a memory space opened by the main function to the sub-function. The sub-function can directly read and write the memory space on the main function. When the sub-function ends, this part of space will not be released, and the main function will continue to use this space!

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.