当前位置:网站首页>C language functions (2)
C language functions (2)
2022-07-26 03:25:00 【Ants look up to the sky】
List of articles
Preface
Before writing the official content , First, check the last blog (http://t.csdn.cn/JRw3H) Add some knowledge :
Function declaration and definition
Declaration of functions
The function declaration text is defined as follows :
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 satisfy the requirement of declaration before use .
The code sample is listed in the last blog , Therefore, I will not repeat .
Definition of function :
The definition of a function refers to the concrete implementation of a function , Explain the function realization .
The figure above shows the graphic display of function definition .
The parameters of the function
There are two arguments to a function , Formal parameters and actual parameters .
Formal parameters ( Shape parameter )
Its definition is as follows :
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 . Formal parameters are automatically destroyed when the function call is completed . So formal parameters are only valid in functions .
Formal parameters are only valid within the scope of the function , Automatically destroy after function call , So the formal parameter names of any function can be the same , It can also be the same as the variable name of the main function . The code example is as follows :
#include <stdio.h>
int add(int x, int y)
{
int result = x + y;
return result;
}
int main()
{
int x, y;
scanf("%d%d", &x, &y);
int c = add(x, y);
printf("%d", c);
return 0;
}
Code above , The variables of the main function are x and y, The formal parameter of a function can also be x,y, The program doesn't report errors .
The actual parameter
Its definition is as follows :
The parameters that are actually passed to the function , It's called the actual parameter .
The argument can be : Constant 、 Variable 、 expression 、 Functions, etc .
Whatever the type of argument is , When you make a function call , They all have to have certain values , In order to pass these values to the parameter .
Pictured above , Among them add(a,b) It can also become add(3,4)、add(1+2,1+3) or add(add(1,2),add(1,3)). These replacement programs will not report errors .
Nested calls and chained access to functions
Functions and functions can be combined according to actual needs , That is, calling each other .
Nested calls
Here is a code example :
#include <stdio.h>
void new_line()
{
printf("hehe\n");
}
void three_1ine()
{
int i = 0;
for (i = 0; i < 3; i++)
{
new_line();
}
}
int main()
{
three_1ine();
return 0;
}
As shown above in three_1ine() Called in the function new_line() function , This is the nested call of a function . The following is an example .
In addition, pay attention to , Functions can be called nested , But you can't nest definitions .
Chained access
The definition for :
Take the return value of one function as the parameter of another function . The following is a code example :
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "he11o";
int ret = strlen(strcat(arr, "bit"));// Chained access to functions
printf("%d\n",ret);
return 0;
}
If the reader does not know the above procedure strlen Functions and strcat The purpose of function must be searched ( There are many library functions , Some library functions that are not commonly used and have not been seen must give play to the power of search engines , Don't ignore it because you don't often use it or haven't seen it ).strcat The string returned by the function will be treated as strlen The parameters of the function , This is the chained access of functions . Execution results :
Example 2:
#include <stdio.h>
int main()
{
printf("%d",printf("%d",printf("%d",43)));
return 0;
}
Let's expand here , In the above code example printf The return value of the function is Number of numbers , Such as printf(“%d”,43), It will print first 43, Then return 2, Then the previous one printf Can print numbers 2 Then return 1, final printf Can print numbers 1, Then return 1.
The results are as follows :
Function recursion
What is recursion ?
1. The programming skill of program calling itself is called recursion .
2. Recursion as an algorithm is widely used in programming languages . A procedure or function has the function of directly or indirectly calling itself in its definition or description
In popular language , Function recursion is a function that calls itself .
Two necessary conditions for using recursion :
1. There are restrictions , When this constraint is met , Recursion doesn't continue .
2. After each recursive call, it gets closer and closer to this constraint .
If recursion does not meet these two conditions , Then you will program dead recursion , The program will run all the time , Finally, it was forcibly terminated .
Let's start with an example
#include <stdio.h>
int main()
{
printf("hehe\n");
main();
return 0;
}
The above is a dead recursion main() Functions are always using themselves , But there are no termination conditions .
Then let's look at the principle of recursion :
#include <stdio.h>
void print(int n)
{
if (n > 9)
{
print(n / 10);
}
printf("%d ", n % 10);
}
int main()
{
unsigned int num = 0;
scanf("%d", &num);
print(num);
return 0;
}

The above code is a simple recursion . Let's explain recursion with pictures and texts :
Finally, expand
Not void function , If you don't write the return value , The function will return a value by default .
Some compilers return : The result of the last instruction .
The code is as follows :
#include <stdio.h>
int test()
{
int a = 10;
int b = 20;
int c = a + b;
}
int main()
{
printf("%d\n", test());
return 0;
}

The final production is not easy , If you feel good, please click one button three times , The author is welcome to point out the mistakes in the comment area or private letter .
边栏推荐
- 爆肝出了4W字的Redis面试教程
- Completion report of communication software development and Application
- els 注册窗口类、创建窗口类、显示窗口
- Opencv saves pictures in the specified format
- Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
- UDP和TCP可以使用同一个端口吗?
- Managing databases in a hybrid cloud: eight key considerations
- What are the methods of array sorting in JS
- 实现一个方法,找出数组中的第k大和第m大的数字相加之和
- C语言预处理指令以及Makefile脚本讲解
猜你喜欢

Leetcode · daily question · 919. complete binary tree inserter · hierarchy traversal · BFS

C语言函数(2)

Intensive reading of the paper -yolov1:you only look once:unified, real time object detection
![[STL]优先级队列priority_queue](/img/79/d13913cbb9d98f936a9501633b38bf.png)
[STL]优先级队列priority_queue

Uncaught TypeError: $(...).onmouseenter is not a function js错误,解决办法:

使用anaconda配置gpu版本的tensorflow(30系列以下显卡)

Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation

Course notes of single chip microcomputer principle and interface technology for migrant workers majoring in electronic information engineering
![[untitled]](/img/6f/a2cd98af7a8de469e5311422b48afe.png)
[untitled]

使用VRRP技术实现网关设备冗余,附详细配置实验
随机推荐
Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting
ELS callback function, exit message
URDF 语法详解
Managing databases in a hybrid cloud: eight key considerations
Mbr3045ct Schottky diode, mbr0100, mbr2060ct diode parameters
[stl] priority queue priority_ queue
QT笔记——临时的悬浮窗口
els 窗口设置、WM_CREATE、WM_PAINT
canvas——绘制曲线——挂钟,饼图,五角星
6-40v input fixed 5V 3.3V output 1.1a current 23-5 package
canvas——绘制图片——动图制作
Pit trodden when copying list: shallow copy and deep copy
ELS modify cursor, modify Icon
Jsd-2204-cool shark Mall (Management Commodity module) -day02
【虚拟化】查看vCenter和ESXi主机的Log日志文件
Opencv error: (parameter or structure field)) unrecognized or unsupported array type in functon 'cvgetmat‘
爆肝出了4W字的Redis面试教程
LDP相关知识点
使用VRRP技术实现网关设备冗余,附详细配置实验
Docker installs redis!!! (including detailed illustration of each step) actual combat