当前位置:网站首页>[C language] summary of function knowledge points
[C language] summary of function knowledge points
2022-07-19 09:36:00 【An ran_】
List of articles
- One 、 Understanding of relevant terms
- 1. What is a function
- 2.C Functions in language
- 3. Function parameter
- 4. Function call
- (1) Value transfer call
- (2) Address call
- 5. Nested calls and chained access to functions
- (1) Nested calls
- (2) Chained access
- 6. Function declaration and definition
- (1) Function declaration
- (2) Function definition
- 7. Function recursion
- (1) What is recursion
- (2) There are two necessary conditions for recursion
- (3) Advantages and disadvantages of recursion and iteration
- Two 、 Error prone example demonstration
One 、 Understanding of relevant terms
1. What is a function
yes Subroutines , Compared to other codes , have Relative independence .
Tend to have Input parameters And there are Return value .
2.C Functions in language
Ι. Library function
(1) Common library functions
·IO function
· String manipulation functions
· Character manipulation functions
· Memory manipulation function
· Time / Date function
· Mathematical functions
· Other library functions
(2) Be careful
1. Use library functions , Must contain #include Corresponding header file .
2. Use document learning library functions —— Focus on two points 【 return type , Parameter type 】
3. Common ways to learn library functions
①C/C++ Language website 【 On-line 】
②MSDN【 offline 】
Π. Custom function
A function with specific functions encapsulated by programmers according to their own needs .
In general , Wide applicability 、 Strong independence .
There are three main points to consider Function name 、 return type 、 Function parameter .
3. Function parameter
(1) The actual parameter
The parameters that are actually passed to the function , It's called the actual parameter .
The argument can be : Constant 、 Variable 、 expression 、 Functions, etc .
To make a long story short , Is a variable or constant with a definite value .
(2) Formal parameters
Formal parameters refer to the variables in brackets after the function name , Because formal parameters are only instantiated when the function is called ( Allocate memory units ), So it's called formal parameter .
※ Be careful
1. After the formal parameter is instantiated, it is actually equivalent to a temporary copy of the argument .
2. Formal parameters are automatically destroyed when the function call is completed
【 Review the process of creating and destroying function stack frames 】
4. Function call
(1) Value transfer call
The actual parameter is Non address .
After call , No effect on arguments .
Be careful : Passing the array name is essentially passing the address of the first element of the array , It belongs to address calling .
(2) Address call
The actual parameter is Address .
After call , Will affect the arguments .
5. Nested calls and chained access to functions
(1) Nested calls
A nested call is a function that calls another function .
Functions can be called nested , But you can't nest definitions .
(2) Chained access
Chained access means that the parameter of one function is the return value of another function .
6. Function declaration and definition
(1) Function declaration
1. Tell the compiler that there is a function called , What are the parameters , What is the return type . But does it exist , Function declarations do not determine .
2. The declaration of a function usually precedes the use of the function . To meet the Declare before use .
3. The declaration of a function is usually placed in the header file
(2) Function definition
The definition of a function refers to the concrete implementation of a function .
7. Function recursion
(1) What is recursion
To put it bluntly , Recursion is a programming skill of calling functions by themselves .
(2) There are two necessary conditions for recursion
The two conditions actually correspond to the two stages of recursion .
① There are restrictions , When this constraint is met , Recursion doesn't continue .—— Delivery stage
② After every recursive call Getting closer to This restriction —— Changes in the implementation process .—— Return stage
(3) Advantages and disadvantages of recursion and iteration
Iteration can be simply understood as a cycle for the time being .
| Definition | advantage | shortcoming | |
|---|---|---|---|
| recursive | The programming skill of program calling itself is called recursion | 1) Big problems turn into small problems , Can greatly reduce the amount of code ; 2) Define an infinite set of objects with finite statements .; 3) The code is more concise and clear , Better readability | 1) Call function recursively , Waste space ; 2) Recursion is too deep to overflow the stack ; |
| iteration | Use the original value of the variable to calculate a new value of the variable , Iteration is A Keep calling B | 1) High iteration efficiency , Running time only increases with the number of cycles 2) There's no extra cost , There is no increase in space , | 1) Not easy to understand ; 2) Code is not as simple as recursion ; 3) It's hard to write complex questions |
The connection between the two :1) There must be iterations in recursion , But iterations don't have to be recursive , Most of them can switch to each other .
2) Can be used iteratively without recursion , Call function recursively , Waste space , And recursion is too deep to cause stack overflow ./ relative /
Two 、 Error prone example demonstration
1. Nested calls and chained access to functions
Nested call instances
#include <stdio.h>
void new_line()
{
printf("hehe\n");// Nested call printf function
}
void three_line()
{
int i = 0;
for(i=0; i<3; i++)
{
new_line();// Nested call new_line function
}
}
int main()
{
three_line();// Nested call three_line function
return 0;
}
Chain access instance
// example 1
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "hello";
int ret = strlen(strcat(arr,"bit")); //strcat Return the array name after splicing
printf("%d\n", ret);
return 0;
}
// example 2
#include <stdio.h>
int main()
{
printf("%d", printf("%d", printf("%d", 43)));
// The result is 4321
// notes :printf The return value of the function is the number of characters printed on the screen
return 0;
}
2. Function declaration and definition
The header file
test.h**** The content of
Place the declaration of the function
#ifndef __TEST_H__
#define __TEST_H__
// Declaration of functions
int Add(int x, int y);
#endif //__TEST_H__
Source file
test.c**** The content of
Implementation of placement function
#include "test.h"
// function Add The implementation of the
int Add(int x, int y) {
return x+y; }
3. Function recursion
1) Print each bit of the unsigned number in sequence
#include <stdio.h>
void print(int n)
{
if(n>9)
{
print(n/10);
}
printf("%d ", n%10);
}
int main()
{
int num = 1234;
print(num);
return 0;
}
2) Use recursion and do not create temporary variables to implement strlen
#incude <stdio.h>
int Strlen(const char*str)
{
if(*str == '\0')
return 0;
else
return 1+Strlen(str+1);
}
int main()
{
char *p = "abcdef";
int len = Strlen(p);
printf("%d\n", len);
return 0;
}
边栏推荐
- Line Flow Based Simultaneous Localization and Mapping
- 使用 Golang 正确处理五大互联网注册机构的 IP 数据
- 05---增透膜
- 第十二章 STL 之 list
- Simple third-party component log desensitization
- OLED显示如何理解 12*6、16*8、24*12等字符大小
- Part I - Fundamentals of C language_ 6. Function
- LDA classifier
- Two structures ifconf and ifreq
- Microservice splitting for stand-alone projects
猜你喜欢
随机推荐
组件间的相互访问
Chapter IX deque of STL
第十三章 STL 之 set/ multiset
Mutual access between components
Anaconda与Jupyter Notebook入门级详细使用教程
Line Flow Based Simultaneous Localization and Mapping
How to synchronize historical data when MySQL is upgraded to primary and standby?
pip和pip3的区别用法详解
MySQL 视图
Day 5 training
【网络研究院】机器学习系统的威胁是时候该认真对待了
Daily model series: July 11, 2022
MySQL 用户管理
Go-Excelize API源码阅读(二)——OpenFile()
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
【性能优化方法论系列】六、总结
使用 Golang 正确处理五大互联网注册机构的 IP 数据
After working hard, I found that there were so many messes around
C语言基础篇 —— 2-3 指针与数组
R语言data.table导入数据实战:data.table使用dcast.data.table函数实现透视表(pivot table)









![[troubleshooting] common problems and solutions when installing MySQL in Windows system](/img/45/339bda7ecf8879999d8ff128c1d3ab.png)