The powerful function of C language function and the experience of getting into the pit

       In the previous learning process, the expression of many functions is directly implemented in the main function, which will cause the main function body to look redundant and difficult to understand. For beginners, there are generally not too many functions implemented, so it will not Realize the superiority of the function. If a project needs to implement complex functions, the designer writes all the implementation process in the main function. Due to the substantial increase in the amount of code, the efficiency of code operation will decrease, and the later code modification will be optimized. It will also increase a certain degree of difficulty. So gradually learning to use functions to implement and specific functions will be more helpful to our future learning.

For example, if we want to write a simple code that outputs a leap year from 1900 to 2021, first use a loop in the main function to achieve the output from 1900 to 2021, and design a leap year judgment function outside the main function. Calling it in the main function can realize the display output of leap years. The advantage of this use is that if you want to judge other years, you only need to call this function, and you don't need to rewrite the code every time to reduce efficiency.

Speaking of how good and easy to use functions are, now I can simply say that I have many pitfalls in function design and calling, and I often write bugs accidentally, and often I can't find out what went wrong. Let’s also use some practical exercises to talk about. The first time I was pitted was to use a function to write the exchange between two data. If you don’t use a function, it is very easy to achieve, which is to quote an empty variable. To achieve the exchange of two data, the general idea of ​​using the function is basically the same. The actual parameter data in the main function is passed to the formal parameter reception in the function, so as to realize the data replacement in the function, but the practice tells us to directly put the actual parameter The function passed to the formal parameter cannot be achieved. Later, after debugging, it was found that the instantiation of the formal parameter is equivalent to a temporary copy of the actual parameter. The formal parameter has an independent memory space, so it cannot be exchanged. If you want to achieve it, you need to use a pointer in the function to receive the address of the variable, and use the dereference operation of the pointer to point to the value in the address for exchange, because the pointer reception does not form an independent space, and the pointer can be used Data access to realize the function. In addition, the array parameter only passes the address of the first element of the array, not the entire array element. In essence, the array in the function is a pointer to the first address of the element. Therefore, the number of array elements cannot be calculated in the function. The process only Can be outside the function body.

The reference code is as follows:

#include<stdio.h>


void swap(int* pa, int* pb)//Use pointer to create a data replacement function, pa receives the address of a, pb receives the address of b


{

int tmp = 0;//Define a zero variable

tmp = *pa;//Dereference pointer data value to realize data exchange

*pa = *pb;

*pb = tmp;

}

int main ()

{

int a = 19;

int b = 90;

printf("%d %d\n", a, b);

swap(&a, &b);//Call the data exchange function to realize the exchange

printf("%d %d\n", a, b);

return 0;

}


Guess you like

Origin blog.51cto.com/15101214/2675429