当前位置:网站首页>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
funcAnd get the function address&funcThe 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 .
边栏推荐
- 塔米狗 热门资讯:厦门产权交易中心再创第一次!
- [untitled]
- Setting of Seaborn drawing style
- cartographer_ ceres_ scan_ matcher_ 2d
- 【深入理解TcaplusDB技术】Tmonitor后台一键安装
- Kubernetes快速實戰與核心原理剖析
- 【深入理解TcaplusDB技术】TcaplusDB导入数据
- [Anxin cup 2019]easy_ web-1
- Efficient remote office manual | community essay solicitation
- PostgreSQL logical storage structure
猜你喜欢

【深入理解TcaplusDB技术】TcaplusDB业务数据备份
![[Anxin cup 2019]easy_ web-1](/img/1d/f164c220f6c8e98b981ef79b0e96bc.png)
[Anxin cup 2019]easy_ web-1

5000字解析:实战化场景下的容器安全攻防之道

Tami dog sharing: the way of property right transaction and the significance of data-based property right transaction market

分布式事务处理方案大 PK

塔米狗分享:产权交易的方式及其产权交易市场数据化意义

SCCM基于已安装的 APP创建客户端集合并定期推送应用更新

高效远程办公手册| 社区征文

Highly available configuration of database (MySQL)

【深入理解TcaplusDB技术】Tmonitor后台一键安装
随机推荐
《预训练周刊》第50期:无解码变换器、神经提示搜索、梯度空间降维
用时间戳优化 TCP 实践
Shell process control - 35. Multi branch case conditional statements
What do you mean by concurrency, concurrency, and high concurrency?
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
Centos7 deploying MySQL environment
Pre training weekly (50): no decoding converter, neural prompt search, gradient space dimensionality reduction
微证券开户正规安全吗,怎么操作开户?
Using slurm cluster computing node debugger in vscode
Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关
Two common schemes for handling interface idempotence
3. operator
6. functions
Questions and answers No. 43: application performance probe monitoring principle PHP probe
MySQL约束(创建表时的各种条件说明)
如何使用搜索引擎?
PHP uses grafika to synthesize pictures and generate poster images
Modification method of EKF extended Kalman filter for omnidirectional ground
What is Devops in an article?
An error "\.\global\vmx86" is reported when vmware12 virtual machine is opened: the system cannot find the specified file.