当前位置:网站首页>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)自定义函数
库函数并不是能做所有的事情,还是需要程序员来做一些“自定义函数”来完善程序中所需要的功能的。自定义函数和库函数一样,都是有函数名、返回值类型以及函数参数的。但是不一样的是,这些都需要我们自己来设计。
边栏推荐
- Liunx installing MySQL
- redis 报错解决与常用配置
- 欧洲的龙之城|国家地理全球最浪漫最安全的目的地
- Shell (34): time
- LinkedList source code analysis
- How much do you know about the cause of amplifier distortion?
- 5 minutes to quickly launch web applications and APIs (vercel)
- 2021-04-14
- [Li mu] how to read papers [intensive reading of papers]
- How to change the dial on the apple Watch
猜你喜欢

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

What are the indicators, dimensions and models in Business Intelligence BI data warehouse?

In a frame because it set 'X-FRAME-OPTIONS' to' deny '
![[path planning] week 1: hodgepodge](/img/8b/d7c370b0deac33c41a72f8105ea357.png)
[path planning] week 1: hodgepodge

redis 报错解决与常用配置

In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!
![[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design](/img/5d/71c75a3622f7814f385d04b6148074.png)
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
![[ROS introduction] cmakelist Txt and packages XML interpretation](/img/37/24ce4be244ea56c2c84342492fccd4.png)
[ROS introduction] cmakelist Txt and packages XML interpretation

The link added in the bottom menu cannot jump to the secondary page

NFT can only be viewed from afar, but not blatantly played
随机推荐
【ROS】ROSmsg cakin_ Make compilation error
Half optimized SQL
R language data Table data import practice: data Rename the name of the table data column (rename)
Shell (34): Time
Fundamentals of shell programming (Part 7: branch statement -if)
322. change exchange
Note: by the end of 2022, the printing entrance of Guangdong second-class cost engineer's admission card has been opened
Which securities company is the safest and best choice for stock trading account opening
5分钟快速上线Web应用和API(Vercel)
≥ server2012r2 system, disable some planned tasks of the system
Greedy interval problem (1)
2021-03-06
How to improve work efficiency? Macintosh efficiency tool set
Redis distributed lock
How to continuously improve performance| DX R & D mode
[GWCTF 2019]mypassword XSS
Next permutation [give play to subjective initiative to discover laws]
In the middle of the year, we will promote the integration of worry free, and the value-added package will be reduced by 6
【ROS】ROSmsg cakin_make编译错误
下一个排列[发挥主观能动性发现规律]