当前位置:网站首页>Part I - Fundamentals of C language_ 6. Function
Part I - Fundamentals of C language_ 6. Function
2022-07-19 09:33:00 【qq_ forty-three million two hundred and five thousand two hundr】
6.1 summary
6.1.1 The function classification
C A program is made up of functions , The code we write is composed of the main function main() Started . The function is C The basic modules of the program , Is a unit of program code used to accomplish a specific task .
From the perspective of function definition , Functions can be divided into system functions and user-defined functions :
- System function , Library functions : This is provided by the compilation system , Users don't have to define these functions themselves , You can use them directly , Such as our commonly used print function printf().
- User defined function : To solve the special needs of users .
6.1.2 Function function
- The use of function can save repeated code , Reduce code duplication
// Find the maximum of two numbers
int max(int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
int main()
{
int a1 = 10, b1 = 20, c1 = 0;
c1 = max(a1, b1); // call max()
printf("%d\n",c1);
return 0;
}
2. Functions can make programs more modular , This is conducive to the reading of the program , To modify and perfect
6.1.3 Function call : Generate random numbers
When a function is called , Need to care 5 elements :
- The header file : Contains the specified header file
- Function name : The function name must be the same as that declared in the header file
- function : You need to know what this function can do before calling
- Parameters : Parameter types should match
- Return value : Receive return values as needed
6.2 Definition of function
6.2.1 Function definition format
The general form of function definition :
Return type Function name ( List of formal parameters )
{
Data definition part ;
Execute statement part ;
}

6.2.2 Function name 、 Shape parameter 、 The body of the function 、 Return value
1) Function name
Theoretically, you can choose a name at will , The best name is known by name , Users should know the function of this function when they see its name . Be careful , There is a circle substitution after the function name (), This represents a function , It's not an ordinary variable name .
2) Parameter list
Formal parameters specified when defining functions , When there is no function call , They don't take up storage units in memory , So they are called formal parameters or virtual parameters , It's called formal parameter for short , Indicates that they are not actual data , therefore , Variables in formal parameters cannot be assigned .
void max(int a = 10, int b = 20) // error, Formal parameters cannot be assigned
{
}
3) The body of the function
Curly braces { } The content in is the content of the function body , Here for Function function implementation process , This is not much different from previous code writing , Before, we wrote the code in main() In the function , Now just write these into other functions .
4) Return value
The return value of the function is passed through return The statement gets ,return The following value can also be an expression .
- Try to make sure that return The value of the expression in the statement and the return type of the function are of the same type .
int max() // The return value of the function is int type
{
int a = 10;
return a; // Return value a by int type , Function return type is also int, matching
}2. If the function returns the type and return The value of the expression in the statement is inconsistent , The return type of the function shall prevail , namely Function return type determines the type of return value . For numerical data , Automatic type conversion .
double max() // The return value of the function is double type
{
int a = 10;
return a; // Return value a by int type , It will turn into double Type and then return
}3.return Another function of the statement is to interrupt return The executing function , Be similar to break Break the loop 、switch The sentence is the same .
int max()
{
return 1; // Execute to , The function has been interrupted , So the following return 2 Can't be executed to
return 2; // No implementation
}4. If the function has a return value ,return It must be followed by a value , If the function does not return a value , The function name must be preceded by void keyword , Now , We can also write code through return Interrupt function ( It can also be done without ), Just then ,return There is no content behind ( A semicolon “;” With the exception of ).
void max() // It's better to have void keyword
{
return; // Interrupt function , This is optional
}Ongoing update ...
边栏推荐
猜你喜欢
![[troubleshooting] common problems and solutions when installing MySQL in Windows system](/img/45/339bda7ecf8879999d8ff128c1d3ab.png)
[troubleshooting] common problems and solutions when installing MySQL in Windows system

es概念模型与基本故障
![[hero planet July training leetcode problem solving daily] 17th kuansou](/img/92/9b8e3a710430d37564d7ea3f28168f.png)
[hero planet July training leetcode problem solving daily] 17th kuansou

C语言基础篇 —— 2-1 指针与野指针

Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage

uniapp仓库管理系统源码

第一部分—C语言基础篇_1. C语言概述

【论文笔记】基于深度学习的视觉检测及抓取方法

在Pycharm里面如何避免全局索引?如何取消对于某个文件夹的索引?

Interview questions - design test cases for:: memcpy function
随机推荐
Classification of top essays
[Luogu] p2357 tomb keeper
Componentized advanced -- slot
第十三章 STL 之 set/ multiset
Microservice splitting for stand-alone projects
研究发现DNA纳米设备注射液可安全用于医疗用途
L2-029 independent happiness
【摸鱼神器】UI库秒变低代码工具——表单篇(二)子控件
第一部分—C语言基础篇_3. 运算符与表达式
Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage
Latest fruit flstudio20.9 low version upgrade high version tutorial
实用工具系列 - Xshell安装下载与使用
目标检测模型大小计算,模型复杂度(参数换算公式)
Classificateur knn
C51 常见数据类型详解
el-table 列拖拽(无须引入其他插件)
es概念模型与基本故障
06---光在介质中的特性
C语言基础篇 —— 2-3 指针与数组
Could NOT find CUDA (missing: CUDA_INCLUDE_DIRS) (found suitable exact version “11.4“)