C language - actual parameters and formal parameters of functions_study notes

introduction

The importance of C language and functions in programming

A sentence from the novel "The Fighting Swordsman in the Snow" summarizes the importance of functions in C language: "If heaven had not given birth to me, Li Chungang, the sword would be like a long night." The importance of functions in programming is the same as that of Li Chungang in the sword.

C language functions play an important role in code reuse, modular design, function parameters, function call stack and recursion. Proper use of functions can improve the efficiency, readability, and maintainability of your program.

function components

Functions in C language consist of the following parts:

  1. Function name: The function name is the identifier of the function and is used to call the function in the program. Function names should be descriptive so that it is easy to understand what the function does.
  2. Parameter list: The parameter list includes the type, order and number of input parameters accepted by the function. Each parameter in the parameter list has a type and an identifier.
  3. Function return value type: The function return value type is the data type returned after the function is executed. If the function does not return any value, the return value type is void.
  4. Function body: The function body is the block of statements that contains the specific code block for the function's execution. It defines how the function is executed, including the processing of input parameters and the calculation of return values.

To create a function, it must contain the above basic elements.

The following is a simple example showing the composition of C language functions:

int add(int a, int b)  // 函数名:add,参数列表有两个 int 类型的参数 a 和 b
{
    
       
    int result = a + b;    // 计算 a 和 b 的和,存储到 result 变量中
    return result;         // 返回 result 的值
}

In this example, the function name is add. The parameter list has two int type parameters a and b. The function return value type is int. The function body contains statements that calculate the sum of a and b and store the result in the result variable. Finally, the value of result is returned through the return statement.

In C language, formal parameters and actual parameters are important parts of functions.

Overview of formal parameters

Formal parameter: The full name is "formal parameter". It is a variable listed in parentheses after the function name when the function is defined. It is used to receive the parameters passed when the function is called.

The function of formal parameters is to realize the connection between the calling function and the called function. Usually the data processed by the function, factors affecting the function of the function or the result of function processing are used as formal parameters.

Overview of actual parameters

Actual parameter: The full name is "actual parameter". Actual parameters are the actual values ​​passed to the function when calling the function. They can be constants, variables or expressions.

The role of actual parameters is to provide data required for function execution and is used within the function.

The relationship between actual and formal parameters

in,Actual parameters and formal parameters must be consistent in number, type, and order, otherwise compilation errors will occur.

In C language, the data of formal parameters and actual parametersTypes must be compatible, that is, type conversion can be performed between them.

Specifically, if the formal parameter is a basic data type, such as int, and the actual parameter is of double type, then the actual parameter will convert the double type to the int type through type conversion, and then pass it to the formal parameter.

If the formal parameter is a structure type and the actual parameter is a different structure type, then the actual parameter needs to be converted to the same structure type as the formal parameter through structure assignment or copying.

If the formal parameter is a pointer type and the actual parameter is a different pointer type, then the actual parameter needs to be converted to the same pointer type as the formal parameter through pointer conversion.

In short, when a function is called, the data type of the actual parameter must be compatible with the formal parameter, or can be converted to a compatible type through type conversion.

The difference between formal and actual parameters

life cycle differences

The scope of formal parameters is limited to the inside of the function. Once the function execution ends, the life cycle of the formal parameters also ends.
The scope of actual parameters is usually limited to the period of function call. When the function ends, the life cycle of the actual parameters ends.

From a life cycle perspective, the life cycle of formal parameters is shorter than that of actual parameters.

Differences in storage location

Actual parameters and formal parameters have separate storage spaces and do not share memory between them.

#include <stdio.h>

void func(int a, int b) 
{
    
    
    printf("a = %p, b = %p\n", &a, &b);
}

int main()
 {
    
    
    int x = 10, y = 20;
    printf("x = %p, y = %p\n", &x, &y);
    func(x, y);
    return 0;
}

Insert image description hereAs shown in the figure above, you can see that the addresses of x and y are different from the addresses of a and b.
Insert image description here
As shown in the picture above, x and y do get the values ​​​​of a and b, but the addresses of x and y are different from the addresses of a and b, so we can understand it asThe formal parameter is a temporary copy of the actual parameter

Guess you like

Origin blog.csdn.net/yjagks/article/details/132116681