The function return type is divided into
- Returns the base data type
- (pointer function) returns a pointer type that returns a new pointer to a pointer that returns a worthwhile address//pointer function that cannot return a local amount (variable within a function)
- Returns the reference type, and the address//reference function that returns the return value directly cannot return a reference to the local amount (variable within the function) without generating a copy. Returning a reference is more straightforward and more readable than returning a pointer
Types of Functions refers to the interface of a function, including the parameter definition and return type of a function
such as the function double Max (double,double);
Double (double,double);
Defining methods
typedef type function type name (formal parameter table)
such as: typedef double Functiontype (double,double);
Use:
such as: Functiontype max,min,average;
(equivalent to declaring three functions at the same time double Max (double,double);d ouble min (double,double); double average (double,double);)
function Pointers is a pointer variable that points to a function
Defining methods
- Type (* pointer variable name) (formal parameter list)
- function type * pointer variable name
such as: Double (*FP) (double,double);
Functiontype *FP;
When a function pointer is used as a function parameter, the address of the function can be passed, and different functions are called through the arguments.
Example:
1#include <iostream>2 using namespacestd;3 //Defining the Functiontype function type4typedefDoubleFunctiontype (Double,Double);5 //declaration of three functions min max average6 Functiontype min,max,average;7 //defining function Pointers FP8Functiontype *FP;9 //function callfunction with function pointersTen DoubleCallFunction (Functiontype *,Double,Double); One intMain () A { - DoubleA=4.3, b=3.6; -cout<<"min ="<<min (b) <<"\tmax ="<<max (b) <<"\tave ="<<average (b) <<Endl; the //function pointer get function address -Fp=min; cout<<"*fb min ="<<callfunction (fp,a,b); -Fp=max; cout<<"\T*FB max ="<<callfunction (fp,a,b); -Fp=average; cout<<"\T*FB Ave ="<<callfunction (fp,a,b) <<Endl; +cout<<"Call min ="<<callfunction (min,a,b); -cout<<"\tcall max ="<<callfunction (max,a,b); +cout<<"\tcall Ave ="<<callfunction (average,a,b) <<Endl; A atCin.Get(); - return 0; - } - DoubleMinDoubleADoubleb) - { - returnA>b?b:a; in } - DoubleMaxDoubleADoubleb) to { + returnA>b?a:b; - } the DoubleAverageDoubleADoubleb) * { $ return(A+B)/2;Panax Notoginseng } - DoubleCallFunction (Functiontype *FP,DoubleADoubleb) the { + returnFP (A, b); A}
C + + pointer functions and function pointers