How to pass function parameters in C Language

Source: Internet
Author: User

In C, you can ensure that:

1. All functions use value transfer: in fact, even a pointer is also a value transfer. In C ++, the reference is passed down to the end and the value is passed.

2. The passing process of parameters from real parameters to form parameters is actuallyInitialize (! = Value assignment) Process. When passing parameters, perform some arithmetic expressions and initialize them by values.

3. The scope of the parameter is the function field. It is the same as other automatic variables defined in the function and has no special characteristics.

4. When the function form parameter carries the const qualifier, the real parameter can be passed successfully. This transfer is equivalent to initialization. Once initialized, it cannot be changed.

5. The order in which parameters are transmitted is from right to left.

Example 1: (rule 2)

 
# Include <stdio. h>
 
 
 
VoidFunc (IntIi)
 
{
 
Printf ("In func (): % d \ n", Ii );
 
}
 
 
 
IntMain ()
 
{
IntI = 0;
 
Func (I ++ );
 
 
 
Printf ("Out func (): % d \ n", I );
 
 
 
Return0;
 
}

Result:

In func (): 0
Out func (): 1

It can be seen that when I is passed into the function parameter, it is the process of assigning values.

II = I ++;

Example 2: three small segmentsProgram(Rule 4)

 
# Include <stdio. h>
 
 
 
IntMain ()
 
{
 
IntI = 0;
Const IntJ = I;
 
 
 
Return0;
 
}

This program will not have any problems.

 
# Include <stdio. h>
 
 
 
IntMain ()
 
{
 
IntI = 0;
 
Const IntJ;
 
J = I;
 
 
 
Return0;
 
}

This program may be compiled incorrectly! So you must distinguishInitializationAndAssignmentRelationship

 
# Include <stdio. h>
 
 
 
VoidFunc (Const IntIi)
 
{
 
Printf ("In func (): % d \ n", Ii );
 
}
 
 
 
IntMain ()
 
{
 
IntI = 0;
 
 
 
Func (I );
 
 
Return0;
 
}

This program has no errors.

Example 3: (Rule 5)

 
# Include <stdio. h>
 
 
 
VoidFunc (IntI,IntJ)
 
{
 
Printf ("In func (): I = % d \ n", I );
 
Printf ("In func (): J = % d \ n", J );
 
}
 
 
 
IntMain ()
 
{
 
IntI = 0;
 
 
Func (I, I ++ );
 
 
 
Return0;
 
}

Result:

In func (): I = 1
In func (): J = 0

 
# Include <stdio. h>
 
 
 
VoidFunc (IntI,IntJ)
 
{
 
Printf ("In func (): I = % d \ n", I );
 
Printf ("In func (): J = % d \ n", J );
 
}
 
 
 
IntMain ()
{
 
IntI = 0;
 
 
 
Func (I, ++ I );
 
 
 
Return0;
 
}

Result:

In func (): I = 1
In func (): j = 1

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.