当前位置:网站首页>Part II - C language improvement_ 13. Recursive function
Part II - C language improvement_ 13. Recursive function
2022-07-26 23:26:00 【qq_ forty-three million two hundred and five thousand two hundr】
13.1 Basic concept of recursive function
C Support the implementation of recursive functions through the runtime stack . Recursive functions are functions that call themselves directly or indirectly .
13.2 Ordinary function call
void funB(int b)
{
printf("b = %d\n", b);
}
void funA(int a)
{
funB(a - 1);
printf("a = %d\n", a);
}
int main(void)
{
funA(2);
printf("main\n");
return 0;
}Output results
b = 1
a = 2
mainThe calling process of the function is as follows :

13.3 Recursive function calls
void fun(int a)
{
if (a == 1)
{
printf("a = %d\n", a);
return; // Interrupt function is very important
}
fun(a - 1);
printf("a = %d\n", a);
}
int main()
{
fun(2);
printf("main\n");
return 0;
}Output results
a = 1
a = 2
mainThe calling process of the function is as follows :

13.4 Recursive implementation of string inversion
int reverse1(char *str)
{
if (str == NULL)
{
return -1;
}
if (*str == '\0') // Function recursive call end condition
{
return 0;
}
reverse1(str + 1);
printf("%c", *str);
return 0;
}
char buf[1024] = { 0 }; // Global variables
int reverse2(char *str)
{
if (str == NULL)
{
return -1;
}
if ( *str == '\0' ) // Function recursive call end condition
{
return 0;
}
reverse2(str + 1);
strncat(buf, str, 1);
return 0;
}
int reverse3(char *str, char *dst)
{
if (str == NULL || dst == NULL)
{
return -1;
}
if (*str == '\0') // Function recursive call end condition
{
return 0;
}
reverse3(str + 1);
strncat(dst, str, 1);
return 0;
}边栏推荐
- Easily implement seckill system with redis! (including code)
- kalibr标定realsenseD435i --多相机标定
- HCIA-R&S自用笔记(23)DHCP
- 云原生微服务第一章之服务器环境说明
- [H5 bottom scrolling paging loading]
- What is Base64?
- Pyqt5 how to set pushbutton click event to obtain file address
- 公有云安全性和合规性方面的考虑事项
- Is test development development development?
- [postgresql]postgresqlg use generate_ Series() function completes statistics
猜你喜欢
电脑开机后内存占用过高(50%以上)

Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
![[postgresql]postgresqlg use generate_ Series() function completes statistics](/img/62/893986eb97a61f4e9ef32abc8d2a90.png)
[postgresql]postgresqlg use generate_ Series() function completes statistics

2. Realize the map of navigation bar and battle page

Three person management of system design

Ribbon load balancing

Practical project: boost search engine

Sign up now | frontier technology exploration: how to make spark stronger and more flexible

Do you know the common core types of magnetic ring inductors?

基本的SELECT语句
随机推荐
The interviewer asked: this point of JS
Use Arthas to locate online problems
基本的SELECT语句
Sign up now | frontier technology exploration: how to make spark stronger and more flexible
实战项目:Boost搜索引擎
Application of workflow engine in vivo marketing automation | engine 03
HCIA-R&S自用笔记(20)VLAN综合实验、GVRP
Huawei atlas900 reveals the secret: it integrates thousands of shengteng 910 chips, and its computing power is comparable to 500000 PCs!
Lesson 2 of Silicon Valley classroom - building project environment and developing lecturer management interface
什么是 Base64 ?
2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ )
面试官问:JS的this指向
My SQL is OK. Why is it still so slow? MySQL locking rules
Several inventory terms often used in communication
[Luogu] p2341 popular cattle
[postgresql]postgresqlg use generate_ Series() function completes statistics
程序员成长第二十九篇:如何激励员工?
菜鸟网络面试【杭州多测师】【杭州多测师_王sir】
8 other programming languages -- Recording
科研太忙无法顾家?陈婷:人生不能只有一个支点