当前位置:网站首页>Introduction to C language functions
Introduction to C language functions
2022-07-27 05:37:00 【I'm really not a second dimension】
C Introduction to language functions
function
A piece of code with a certain function , yes C The smallest unit of management code in a language
Encapsulate the code into functions , It is convenient to manage and call code
Classification of functions
1. Standard library functions
from C The language standards committee is C Language provides some basic functions in the form of functions , Be encapsulated libc.so In the library , When using, you need to include the corresponding header file , Through the corresponding function name ( Actual parameters ) Method can call functions in the standard library
For example, the following are functions in the standard library
libm.so
#include 《time.h>
time_t time(time_t *tloc);
function : Return from 1970-1-1 0:0:0 How many seconds have elapsed until the function is executed
time_t sec =time(NULL);
#include <stdlib.h>
int system(const char *command);
function : Execute system commands
system("clear");
int rand (void);
function : Return value a random number
int num=rand();
void srand(unsigned int seed);
2. System function
It is a series of functions provided by the operating system in the form of function interface , But it's not really a function
Memory management 、 file management 、 Input and output of files 、 Process management 、 signal processing 、 Process of communication 、 Thread management 、 Thread synchronization 、 Network communication, etc
3. Third party library functions
Open source or paid code base provided by a third party
MD5 encryption algorithm
Json serialize 、 Deserialization
Xml Profile parsing
4. Custom function
In order to better manage your own code 、 Reduce code redundancy and encapsulate the code into functional form
Function declaration :
The purpose of function declaration is to tell other code the calling format of the functionImplicitly declare : When the function is called , There is no previous declaration or definition of this function , An implicit warning will be generated , Parameters will be guessed based on actual parameters , The return value will be guessed as int type
Don't generate implicit declarations anyway , As long as there is a declaration or definition before the callreturn type Function name ( type 1 The name of the parameter 1, type 2 The name of the parameter 2,…);
Be careful :
1、C Function names in are generally all lowercase , Underline to separate
2、 If the return value does not need , Then write void
3、 If you don't need formal parameters , It is suggested to also write void
4、 Even if the formal parameter types are the same , Add the type name to eachFunction definition : Function implementation code
return type Function name ( type 1 The name of the parameter 1, type 2 The name of the parameter 2,…)
{
// The body of the function
return [val];
}
3. Function call :
Function name ( Actual parameters );
Be careful :
1、 There is a function definition before the function call , Then the function declaration can be omitted
2、 The return value of the function will be returned at the call location , Can be displayed immediately , You can also use variables to record
3、 A function is recommended not to exceed 50 Line code
4、 The content of the declaration should be completely consistent with the definition
5、 If no return value is required 、 Parameters , Must write void
6、 Before calling , No statement 、 Definition , Warnings that generate implicit declarations
Function arguments
- The shape parameter 、 The variables defined in the function only belong to the function it is in , This function cannot be used anymore
- Ordinary real participants transfer data between formal parameters by assignment ( One way value passing )
3. return It's actually storing data in a public area ( Areas that can be accessed between functions ), If you don't write return sentence , Then the original value of this area will be read , Get a garbage data - When an array is used as an argument to a function , The length in brackets will be lost , You need to add an extra variable to pass the length of the array
- When an array is passed as a function parameter , Passing is the first address of the array , be called " Address transmission ", Functions and callers of functions can share the same array
Suggestions for designing functions
1、 A function is best to solve a problem , Reduce the error rate , Improve readability
2、 Try to reduce the number of dependency layers between functions ( Reduce coupling )
3、 The data is provided by the caller , The result is returned to the caller ( Improve the generality of functions )
4、 Consider the illegal parameters of the function , You can tell the caller that your parameters are wrong by returning values , You can also write the situation in the form of notes ( Improve the robustness of functions )
边栏推荐
猜你喜欢
随机推荐
用pygame自己动手做一款游戏01
Flask请求数据获取与响应
C语言函数入门介绍
pytorch安装新坑
Three waiting methods of selenium and three processing methods of alert pop-up
Numpy data type conversion
项目登录注册思路
qsort — c语言中自带的排序函数(附带void*、回调函数知识点
分享一道关于#define的选择题(内含#define在预编译时的替换规则,程序环境和预处理相关知识)
Hi3516DV300环境搭建
JWT认证及登录功能实现,退出登录
Redis transaction
上传七牛云的方法
Day3 ---Flask 状态保持,异常处理与请求钩子
while循环
C语言做一个小迷宫
事务,订单系统添加事务
初识C语言——常见的数据类型
Enumeration class implements singleton mode
js进阶知识—函数









