Chapter 7 1
Chapter 7 2
Chapter 7 Review
Chapter 8 1
Chapter 8 2
Chapter 8 3
To use C++ functions, you must complete the following tasks:
- Provide function definition
- Provide function prototype
- Call functions
How the function returns a value
The function returns the return value by copying it to the specified CPU register or memory unit, and then calls the program to view the memory unit. The return function and the calling function must agree on the type of data stored in the memory unit, and the function prototype will The return value type tells the calling program, and the function defines what type of data the called function should return
Function prototype
The prototype (prototype) describes the interface of the function to the compiler, that is, it tells the compiler the type and number of the return value of the function and the type and number of parameters. The
function prototype is a statement, so it must end with a semicolon.
For example:
double cube(double x);
Variable names can be included or not included in the function prototype
Functions and arrays
Functions and two-dimensional arrays
int (*ar2)[4]; //声明一个指向由4个int组成的数组的指针
int *ar2[4]; //声明一个由4个指向int的指针构成的数组
Function and structure
Transfer structure address
Recursion
Recursion with multiple calls
Function pointer
Get function address
Just use the function name (without parameters). For
example: think() is a function, process(), thought() are both functions
process(think) // Pass the address of the think function to the process function
thought( think ()) // Pass the return value of the think function to the thought function
Declare function pointer
double pam(int); //函数原型,声明了一个 叫做 pam的函数
下面的语句和上面的语句效果是相同的
double (*pf)(int); // pf就是函数指针
double (*pf)(int); //pf points to a function that returns double
double *pf(int); //pf() is a function that returns a pointer-to-double
double pam(int);
double (*pf)(int);
pf = pam;
void estimate(int a,double pam);
void estimate(int a,double (*pf)(int));
Use a pointer to call a function
double pam(int);
double (*pf)(int);
pf = pam;
double x = pam(4);
double y = (*pf)(5);
double z = pf(6);
* Currently, in C++, it is considered that ( pf)() and pf() and pam() these three function call methods are the same