当前位置:网站首页>C language elementary order (VI) function

C language elementary order (VI) function

2022-06-21 13:22:00 Princess Kaka

One . The function prototype

1.1 What is a function prototype ?

The function header starts with ; ending , Construct function prototype .

1.2 How to use function prototypes ?

Function prototypes are usually placed in In the header file perhaps In front of the function that calls it .

1.3 What is the use of function prototypes ?

Tell the compiler the basic information about the functions used in advance ( name 、 Parameter number type 、 Return type ).

1.4 Function pre declaration

  • Have a try
#include <stdio.h>
void func();//  Function pre declaration 
void main(){
    func();
}
void func(){
   printf("Hello World\n");
}

Usually put main() Put it at the front of the code for easy reading , But this will lead to compile time because main() Internal calls to functions with errors or warnings . stay main() This kind of problem can be solved by adding function prototype , Called function pre declaration .

Two . Pointers and functions  

1. Function name  

The function name, like the array name, indicates Address , The difference is that the function name is the starting position of executing the function code , Array is the address of the first element of the array . 

Use the function name directly func And get the function address &func The values obtained are the same .

2. A function pointer  

A function pointer is a pointer variable to a function , That is, it is essentially a pointer variable .

void (*pfunc)();// Function pointer definition ( Priority dereference )
pfunc = &func;// pfunc = func;  assignment 
(*pfunc)();// pfunc();  Function pointer calls 

  example : A function that implements the sum product difference quotient of array elements .

#include<stdio.h>

// Print function : 
void print_array(int arr[],int n){
	int* p = arr;
	while(p<arr+n){
		printf("%d ",*p++);
	}
	printf("\n");
}

// Addition function :
int add_array(int arr[],int n){
	int* p = arr;
	int res = *p;
	while(++p<arr+n){
		res = res + *p++;
	}
	return res;
}

// Subtraction function :
int subtract_array(int arr[],int n){
	int* p = arr;
	int res = *p;
	while(++p<arr+n){
		res = res - *p++;
	}
	return res;
}

// Multiplication function :
int multipy_array(int arr[],int n){
	int* p = arr;
	int res = *p;
	while(++p<arr+n){
		res = res * *p++;
	}
	return res;
}

// Division function :
int divide_array(int arr[],int n){
	int* p = arr;
	int res = *p;
	while(++p<arr+n){
		res = res / *p++;
	}
	return res;
}

int add(int a,int b){
	return a+b;
}
int subtract(int a,int b){
	return a-b;
}
int multipy(int a,int b){
	return a*b;
}
int divide(int a,int b){
	return a/b;
}

// Function as an argument   Callback function  
int operation(int arr[],int n,int (*pfunc)(int,int)){// The return value is int type 
                                                     // Two parameters are int type  
	int* p = arr;
	int res = *p;
	while(++p<arr+n){
		res = (*pfunc)(res,*p);
	}
	return res;
}

int main(){
	// The input terminal : 
	int n;
	scanf("%d",&n);
	int arr[n];
	int* p = arr;
	while(p<arr+n){
		scanf("%d",p++);
	}
	
	print_array(arr,n);
	
/*	printf("%d\n",add_array(arr,n));
	printf("%d\n",subtract_array(arr,n));
	printf("%d\n",multipy_array(arr,n));
	printf("%d\n",divide_array(arr,n));  */
	// Define an array of function pointers : 
	int (*pfunc[4])(int arr[],int n) = {add_array,subtract_array,multipy_array,divide_array};
	for(int i =0;i<4;++i){
		printf("%d\n",pfunc[i](arr,n)); 
	}
	
/*	printf("%d\n",operation(arr,n,add));
	printf("%d\n",operation(arr,n,subtract));
	printf("%d\n",operation(arr,n,multipy));
	printf("%d\n",operation(arr,n,divide));  */
	int (*opt[])(int,int) = {add,subtract,multipy,divide};
	for(int i =0;i<4;++i){
		printf("%d\n",operation(arr,n,opt[i])); 
	}
	
}

3. Pointer function

A pointer function is a function with a pointer , That is, the essence is a function , A function return type is a pointer to a type . 

原网站

版权声明
本文为[Princess Kaka]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211055027232.html