当前位置:网站首页>c语言---17 函数简介
c语言---17 函数简介
2022-06-22 20:45:00 【要努力丫!】
对于“函数”,需要掌握的知识有以下内容:
- 函数的定义
- 库函数
- 自定义函数
- 函数参数
- 函数调用
- 函数的嵌套调用和链式访问
- 函数的声明和定义
- 函数递归
1、函数是什么?
在数学中常常看到函数的概念并且运用,如f(x)=2x。那么在C语言中的函数是什么定义呢?又是怎么用呢?在维基百科中对函数的定义是:子程序。(也就是说,子程序就是函数)
- 在计算机科学中,子程序(subroutine,procedure,function,routine,method,subprogram,callable unit),是一个大型程序中的某部分代码,由一个或多个语句块组成。它负责完成某项特定的任务,而且相较于其它的代码,具备相对的独立性。
- 函数一般会有输入参数并有返回值,提供对过程的封装和细节的隐藏,这些代码通常被集成为软件库。

上图的意思是对于某函数,需要有输入参数才可以得到输出参数,在用的时候不用关注该函数的实现过程以及细节等,知道它的功能直接拿过来用就行。
2、C语言中函数的分类
C语言中的函数分为库函数和自定义函数。
(1)库函数为什么会有库函数的存在呢?
在我们学习C语言编程的时候,总是在一个代码编写完成之后就想要在打印结果的窗口看到结果,这个时候我们会频繁地使用一个功能,那就是将信息按照一定的格式打印到屏幕上(这个也是printf的功能);在编程的过程中,我们还可能会频繁地做一些字符串的拷贝工作(strcpy);还会用到计算函数,比如计算n的k次方这样的运算(pow).
像上面描述的这些基础功能,它们不是业务性的代码,在开发的过程中很多人都可能会用到,为了支持可移植性和提高程序的效率,C语言的基础库中一系列类似的库函数,方便进行软件开发。提供几种学习库函数的方法、网址
- cplusplus
- 在线工具MSDN
- cppreference中文显示
C语言常用的库函数 - 输入输出(IO)函数:printf,scanf,getchar,putchar等
- 字符串操作函数:strcmp,strlen等
- 字符操作函数:toupper函数(小写转大写函数)
- 内存操作函数:memcpy,memcmp,memset等
- 时间、日期函数:time等
- 数学函数:sqrt,pow等
- 其它库函数
以cplusplus网址为例,介绍一下如何学习库函数有一点需要特别注意,那就是使用库函数必须包含#include所对一个的头文件
strcpy: char *strcpy (char *destination, const char *source);

示例一:strcpy函数实现字符串的拷贝,将arr2中的内容拷贝到arr1中去
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = {
0 };
char arr2[] = "hello,CSDN";
strcpy(arr1,arr2);//将arr2中的内容拷贝给arr1
printf("%s", arr1);//打印arr1这个字符串 %s---以字符串的格式打印
return 0;
}
运行结果如下:
hello,CSDN
memset: void *memset(void *ptr,int value,size_t num)

示例二:memset函数
memory在平时翻译的时候是被翻译为记忆的,在计算机学科中被翻译为内存,所以memset在C语言中被翻译为“内存设置”。
int main()
{
char arr[] = "hello CSDN";//现在想将arr里面的前五个字符都换成x
memset(arr,'x',5);
printf("%s",arr);
return 0;
}
运行结果:
xxxxx CSDN
(2)自定义函数
库函数并不是能做所有的事情,还是需要程序员来做一些“自定义函数”来完善程序中所需要的功能的。自定义函数和库函数一样,都是有函数名、返回值类型以及函数参数的。但是不一样的是,这些都需要我们自己来设计。
边栏推荐
- A case of 94 SQL optimization (the writing method used is often rejected)
- MySQL multi table operation
- Several methods of changing 91 Oracle common table into partitioned table
- Which securities company is the safest and best choice for stock trading account opening
- 2021-08-21
- Use full function simulation design method
- Mysql database DML operation exercise
- [geometric vision] 4.2 piecewise linear transformation
- Greedy interval problem (2)
- Remote access and control - SSH Remote Management and TCP wrappers access control
猜你喜欢

Remote access and control - SSH Remote Management and TCP wrappers access control

Note: by the end of 2022, the printing entrance of Guangdong second-class cost engineer's admission card has been opened

The required reading for candidates | PMP the test on June 25 is approaching. What should we pay attention to?

Dragon City in Europe | National Geographic the most romantic and safe destination in the world
![[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields](/img/3b/8916c543c149292221ce500209df0d.png)
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields

2021-08-22

2021-08-26
![[geometric vision] 4.2 piecewise linear transformation](/img/1e/a810f4d7e9a6a34647b5cb56fdde67.png)
[geometric vision] 4.2 piecewise linear transformation

Generate detailed API and parameters of QR code using qrcodejs2

The relationship between derivative and differential of function
随机推荐
How to continuously improve performance| DX R & D mode
Shell (34): Time
Dragon City in Europe | National Geographic the most romantic and safe destination in the world
[chess life] 01 life is like chess
Core and semiconductor "RF eda/ filter design platform" shines ims2022
Mysql8 installation and environment configuration
Note: by the end of 2022, the printing entrance of Guangdong second-class cost engineer's admission card has been opened
Rapideye, spot satellite remote sensing image data
【象棋人生】01 人生如棋
Shell (34): time
2021-04-14
2021-08-21
2021-03-06
MySQL multi table operation
Which securities company is the safest and best choice for stock trading account opening
LinkedList 源码解析
. Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
Lua -- iterator, module, meta table
Practice brings true knowledge: the strongest seckill system architecture in the whole network is decrypted. Not all seckills are seckills!!
A case of misuse of append