C/C++ function implementation from zero to bottom

Preface: In C and C++, function can be said to be an important five-star module. Today we will explain functions, focusing on harvesting, and keep up with my rhythm.

Table of contents

1. Function definition template: return type function name (parameter 1, parameter 2, parameter 3...) {function body content and parameters}

2. Function declaration

1) Why is there a declaration?

2) The format of function declaration (take the previous function as an example, put it before use)

3. The establishment and destruction of function stack frames. Next, understand the demonstration code first.

 2) Expand

1) What is a function pointer?

2) Discussion of main function


1. Function definition template: return type function name (parameter 1, parameter 2, parameter 3...) {function body content and parameters}

void sort(int *array){}
void(返回类型)sort(函数名称)(int *array)(函数参数){函数体}

2. Function declaration

1) Why is there a declaration?

Because the computer is executed from top to bottom from the main function, if the function is defined before it is used, there is no need to define it, because the computer has already recognized the function, but if not, the computer will report an error of "unresolved external command". However, such a method is unrealistic when engineering and writing large programs, which will seriously affect the readability of the function. The function declaration is to allow the computer to recognize the function in advance.

2) The format of function declaration (take the previous function as an example, put it before use)

void sort(int* array);

3. The establishment and destruction of function stack frames. Next, understand the demonstration code first.

#include<stdio.h>
int Add(int a, int b) {
	return a + b;
}
int main() {
	int a, b;
	scanf("%d%d", &a, &b);
	int c = Add(a, b);
	return 0;
}

1) The function name is actually the address, and there is only one copy of the function. When the computer runs here, it will find the function through the address, but there are several operations before it enters the function. First, save the current running address, and then save the Make a copy of the parameters by pushing them on the stack (only for passing values), and then enter the function, and after the function is executed, it will be destroyed and popped out of the stack. Next, see my diagram below to understand

 2) Expand

1) What is a function pointer?

Everyone knows that there are integer pointers and floating point pointers. Since the function name is an address, can we use pointers to receive its address? The answer is YES. Now that we know that a function can be received by a pointer, how should we receive it? First, the type of the pointer is the same as the return value of the function, followed by *, and then it should be the name of the pointer. In order to ensure the combination of * and the pointer name, the two need to be combined with parentheses, and the latter is the parameter type of the function, which is not necessary There is the name of the parameter, but the type of the parameter needs to be indicated. Next we show how to write the pointer corresponding to the function

1)
int Add(int a,int b);
//一一对应
int (*ptr)(int ,int )=Add;
2)
int *ButNode(struct Node* List, int n);
//一一对应
int *(*ptr)(struct Node*,int)=BuyNode;

2) Discussion of main function

Although the main function has an additional main word, it is also a function in essence. It is the first function to be executed. The main function is referenced by the existing function of the compilation system. Since it involves the underlying implementation principle, it is difficult to consider It is relatively large. I recommend using VS2019 to observe the operation of the code before the main function. The observation steps are more complicated. I won’t describe too much here. Please refer to the online information to explore by yourself. Next, you can learn about interesting uses of functions, such as function recursion, which has an aesthetic of simplifying complexity.

If you have gained anything, why not join Sanlian? (Follow, like and collect) see you

Guess you like

Origin blog.csdn.net/m0_74316391/article/details/130795536