Functions and function pointers

in conclusion

For a defined function, the function name, function address, and function pointer point to the entry address of the function.

prove

First define the following functions:

/**********************************************************
* 函数:FuncPointer
* 描述:证明函数指针与函数名关系
* 参数:无
* 返回:无
* 备注:
**********************************************************/
void FuncPointer(void)
{
    
    
	cout << "调用FuncPointer函数" << endl;
	cout << FuncPointer << endl;//输出函数名
	cout << &FuncPointer << endl;//输出函数地址
	cout << *&FuncPointer << endl;//输出函数地址指向对象
}

In the main function:

int main()
{
    
    
    FuncPointer();//函数名调用
    (&FuncPointer)();//地址方式调用
    (*&FuncPointer)();//间址方式调用(函数地址指向对象)
 }

Execution result: It
Function name and function pointer
can be seen that the value of the function name, function pointer, and function pointer to the object are equal, and point to the same entry address value. The function can be called through these three methods.

Use of function pointers

It can be seen from the above that a function can be called in the form of a function pointer, so if the function address value is assigned to a pointer variable, the function can be called through this pointer variable. So the question is, how is the type of this pointer defined? To assign the function address to this pointer variable, the type associated with the pointer variable must be the same as the type associated with the function pointer. So we must first understand the type of function.

Function type

  1. Function definition: type function name (formal parameter table); function is composed of three parts: (1) type is the return value type of the function, (2) function name is the address entry of the calling function, (3) formal parameter table is passed into the function Required external parameters. When (1) and (3) of two functions are the same, we call these two functions the same type of function. The following two functions f1 and f2 are functions of the same type.
float f1(float x)
{
    
    
	return (4 / (1 + x * x));
}

float f2(float x)
{
    
    
	return ((float)sqrt(1 + pow(x, 2)));
}
  1. Use typedef to define the function type: typedef type function type name (function parameter list)
typedef float IntegralFunction(float x);
  1. Use defined function types to declare functions or define variables
IntegralFunction f1, f2;//声明f1,f2
  1. Function pointer definition
    (1) function type * pointer variable name;
    (2) type (* pointer variable name) (formal parameter list);
    for example, use the function type defined above to declare a function pointer:
    (1) IntegralFunction *p;
    (2) ) Float (*p)(float);
    When using (2) to define a pointer variable, it should be noted that the brackets on both sides of *p cannot be less. If the brackets are missing, it becomes float *p(float), and p becomes a return type It is a function of int *.

The use of function pointers:

float f1(float x)
{
    
    
	return (4 / (1 + x * x));
}

float f2(float x)
{
    
    
	return ((float)sqrt(1 + pow(x, 2)));
}
/**********************************************************
* 函数:ComputeIntegral
* 描述:求定积分
* 参数:*func - 原函数地址
		a - 积分下限
		b - 积分上限
* 返回:积分结果
* 备注:
**********************************************************/
float ComputeIntegral(IntegralFunction *func, float a, float b)
{
    
    
	const int EdValue = 1000;
	int n = (int)((float)EdValue * (b - a));

	int i;
	float Tn,h;

	h = (b - a) / n;
	Tn = (h * (func(a) + func(b))) / 2;

	for (i = 1; i < n; i++)
	{
    
    
		Tn += h * func(a + i * h);
	}

	return Tn;
}
/**************求函数积分***************/
typedef float IntegralFunction(float x);
IntegralFunction f1, f2;//声明f1,f2
float ComputeIntegral(IntegralFunction* func, float a, float b);

The ComputeIntegral function calculates the size of the definite integral. The passed-in function pointer *func is convenient for different functions of the same type to be passed in. To calculate the definite integral of different functions, you only need to modify the passed-in function pointer. Call the ComputeIntegral function as follows to calculate the definite integral size of f1 in 0-1.

int main()
{
    
    
    float a,b,result;
    cout << "计算定积分" << endl;
    cout << "输入积分下限" << endl;
    cin >> a ;
    cout << "输入积分上限" << endl;
    cin >> b ;
    result = ComputeIntegral(f1,a,b);
    cout << "定积分的值:" << result << endl;
 }

Operation result:
Use of function pointers
With the function pointer, the address of the function can be passed into other functions as a function parameter. This function can call the passed function through the passed function pointer to achieve a specific function.

Guess you like

Origin blog.csdn.net/qq_36439722/article/details/105345450