[C language] The concept of function and function call (parameter passing)

Article Directory

One, the definition of the function

Two, parameter transfer

1. Parameters

2. The way to pass parameters

3. Function call


One, the definition of the function

In the C language, the most basic program module is a function. A program function is regarded as the basic logical unit a C-program consists of a main () function and a normal function of several components.

The syntax format for defining a function is as follows:

Return value type Function name ([parameter type 1 parameter name 1], [parameter type 2 parameter name 2], [parameter type 3 parameter name 3]...)

{

    Function body

   ......

    return return value;
}

Functions mainly include: return value type, function name, parameter type, parameter, function body, return value

Analysis of each part:

  • Return value type: used to limit the function return value type. When the return type is void, the return statement can be omitted.
  • Function name: Indicates the name of the function.
  • Parameter type: used to limit the data type passed into the function when calling the function.
  • Parameters: used to receive the data passed into the function
  • The return keyword: used to end the function and return the return value of the function to the caller of the function.

【note】

 If the function does not require a return value, the return value type of the function should be defined as void, and the return value of the function can be omitted.

[Parameter type 1 parameter name 1], [parameter type 2 parameter name 2], [parameter type 3 parameter name 3]... is called the parameter list , if the function does not need to receive parameters, the parameter list is empty, then the function is called It is a parameterless function .


Two, parameter transfer

1. Parameters

When a program is compiled or run, a certain function is used to complete related functions, which is called a function call . When a function is called, data can be passed through the parameter list of the function. There are two kinds of parameters in the function, which are formal parameters and actual parameters .

(1) Formal parameters

When defining a function, the variable names in parentheses after the function name are formal parameters or virtual parameters, referred to as "formal parameters". For example, the following function declaration statement:

int func(int a,int b);

The variables a and b are formal parameters. Such formal parameters do not occupy real memory, but only exist to identify the parameter list of the function. 

(2) Actual parameters

When a function is called, the parameters in parentheses after the function name are called actual parameters, or "actual parameters" for short. The actual parameters can be constants, variables, and expressions. For example, the following call statement:

func(3,5);

This line of code is a call to the function func, and the data '3' and '5' correspond to the parameter lists a and b, respectively. When a function is called, the formal parameters are real variables and occupy memory space. At this time, the specific data "3" and "5" are passed to the variables a and b in the function parameter list. When the function is called, the formal parameter obtains the data of the actual parameter (equivalent to the assignment). The data is valid at the time of the function call. Once the called function is executed, the value of the formal parameter will be released.

[Note] The data transfer of formal parameters and actual parameters is one-way, which can only be passed from actual parameters to formal parameters, not from formal parameters to actual parameters. 

2. The way to pass parameters

Way transmission parameters are: by value and passed by reference while both ( value transfer and delivery address )

  • When passing actual parameters by value, the program will create a copy of the actual parameters and pass this copy to the called function. Modifications to this copy will not affect the value of the original parameter variables in the calling function.
  • When passing the actual parameter by reference, the calling function will allow the called function to modify the value of the corresponding actual parameter variable.

If the called function does not need to modify the value of the actual parameter variable in the calling function, it should be passed by value. This prevents unexpected side effects (variables are modified).

Pass by reference can only be used when the called function needs to modify the variables in the calling function and the called function is trustworthy.

[Note] In C language, all actual parameters are passed by value. If the passed parameter is an array name, and the subscript is used in the function to refer to the parameter of the array, then the array element in the function is modified in fact the array element in the calling program. The function will access the array elements of the calling program, and the array will not be copied. This is pass by reference, that is, "call by address".

This behavior of arrays seems to be contrary to the pass-by-value rule. But there is no contradiction-the array name is actually a pointer, and a copy of this pointer is passed to the function. Subscript reference is actually another form of indirect access. Indirect access is performed on this copy of the original array.

Case demonstration: swapping two integers

1. Value transfer method

#include <stdio.h>

void swap(int a, int b) {
	int temp = a;
	a = b;
	b = temp;
	printf("交换函数结果:a=%-5d b=%-5d\n", a, b);
}

int main() {
	int a = 3, b = 4;
	swap(a, b);
	printf("主函数交换结果:a=%-5d b=%-5d\n", a, b);
}

operation result:

 

Result analysis:

It can be seen from the above running results that after the swap function is called, only the copies a and b are exchanged, and the actual parameter values ​​are not modified. The value transfer does not affect the value of the original parameter variable in the main function.

2. Address delivery method

#include <stdio.h>

void swap(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
	printf("交换函数结果:a=%-5d b=%-5d\n", *a, *b);
}

int main() {
	int a = 3, b = 4;
	swap(&a, &b);
	printf("主函数交换结果:a=%-5d b=%-5d\n", a, b);
}

operation result:

 

Result analysis:

Pass the pointers of the variables a and b that you want to modify to the swap function. The function modifies the values ​​of a and b through the indirect access operation of the pointer.

3. Function call

(1) The main function calls ordinary functions

(2) Nested call

(3) Recursive call: The function calls itself.

The main function can call ordinary functions, and ordinary functions can call each other, but cannot call the main function.

When calling a function, you need to specify the function name and argument list. The actual parameters and formal parameters must meet three conditions: the number of parameters is equal, the order corresponds, and the type matches

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/108920957