当前位置:网站首页>C语言学习-17-函数作为参数传入函数
C语言学习-17-函数作为参数传入函数
2022-06-22 14:28:00 【阳光九叶草LXGZXJ】
一、开场白
我们经常需要计算某个函数的时间,定位一下到底是哪个函数跑得慢,所以就想以此为例,介绍一下函数如何作为参数传入到函数当中。
二、代码
#include <stdio.h>
#include <time.h>
unsigned long Recursion(unsigned long Num);
void ComputeProcedureTime(unsigned long(*Func)(unsigned long), unsigned long Num);
int main()
{
unsigned long TempNum = 12;
ComputeProcedureTime(Recursion, TempNum);
return 0;
}
void ComputeProcedureTime(unsigned long (*Func)(unsigned long), unsigned long Num)
{
printf("++++++++++++++++++++++++\n");
clock_t start, finish;
clock_t Total_time;
start = clock();
printf("%lu\n", Func(Num));
finish = clock();
Total_time = finish - start;
printf("Elapsed Time : %ld ms\n", Total_time);
}
unsigned long Recursion(unsigned long Num)
{
if (Num == 1)
{
return Num;
}
else
{
return Recursion(Num - 1) * Num;
}
}
三、小口诀
函数作为参数到底怎么写嘞:
(1)我们先把需要传入的函数写上void ComputeProcedureTime(unsigned long Recursion(unsigned long Num), unsigned long Num)。
(2)把unsigned long Recursion(unsigned long Num)的函数名变为(*你想要的名字)。
(3)再把后面的参数名Num去掉。
(4)大功告成,变为了void ComputeProcedureTime(unsigned long (*Func)(unsigned long), unsigned long Num)
四、运行结果
C:\Users\czg> C:\Users\czg\source\repos\x64\Debug\C_TEST.exe
++++++++++++++++++++++++
479001600
Elapsed Time : 1 ms

边栏推荐
- 数据库连接池:压力测试
- Token processing during API encapsulation
- Ros2 pre basic tutorial | Xiaoyu teaches you how to use cmake dependency lookup process
- Charles 乱码问题解决
- 2022年失业的人多吗?今年是不是特别难找工作?
- 口令安全是什么意思?等保2.0政策中口令安全标准条款有哪些?
- Common operations in Visual Studio development
- ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
- Go all out to implement the flood control and disaster relief measures in detail and in place, and resolutely protect the safety of people's lives and property
- Reading of double pointer instrument panel (II) - Identification of dial position
猜你喜欢

Tree structured binary tree

PowerPoint 教程,如何在 PowerPoint 中添加水印?

All famous network platforms in the world

Recommendation of individual visa free payment scheme

全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer

What is the value of a website? Why build an independent station

Using virtual serial port to debug serial port in keil MDK

Charles 乱码问题解决

NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021

天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
随机推荐
得物技术复杂 C 端项目的重构实践
百行代码实现基于Redis的可靠延迟队列
2020年蓝桥杯省赛真题-走方格(DP/DFS)
KEIL仿真和vspd
Live broadcast goes to sea | domestic live broadcast room produces explosive products again. How can "roll out" win the world
DevSecOps: CI/CD 流水线安全的最佳实践
Bochs software usage record
Those confusing user state & kernel state
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
Ml notes matrix fundamental, gradient descent
At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source
Reconstruction practice of complex C-end project of acquisition technology
ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
After 17 years, Liu Yifei became popular again: ordinary people don't want to be eliminated, but they also need to understand this
那些令人懵逼的用户态&内核态
Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
数据库连接池:代码目录
多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer
那些没考上大学的人,后来过的怎样