当前位置:网站首页>第二部分—C语言提高篇_13. 递归函数
第二部分—C语言提高篇_13. 递归函数
2022-07-26 22:20:00 【qq_43205256】
13.1 递归函数基本概念
C通过运行时堆栈来支持递归函数的实现。递归函数就是直接或间接调用自身的函数。
13.2 普通函数调用
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;
}输出结果
b = 1
a = 2
main函数的调用流程如下:

13.3 递归函数调用
void fun(int a)
{
if (a == 1)
{
printf("a = %d\n", a);
return; //中断函数很重要
}
fun(a - 1);
printf("a = %d\n", a);
}
int main()
{
fun(2);
printf("main\n");
return 0;
}输出结果
a = 1
a = 2
main函数的调用流程如下:

13.4 递归实现字符串反转
int reverse1(char *str)
{
if (str == NULL)
{
return -1;
}
if (*str == '\0') // 函数递归调用结束条件
{
return 0;
}
reverse1(str + 1);
printf("%c", *str);
return 0;
}
char buf[1024] = { 0 }; //全局变量
int reverse2(char *str)
{
if (str == NULL)
{
return -1;
}
if ( *str == '\0' ) // 函数递归调用结束条件
{
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') // 函数递归调用结束条件
{
return 0;
}
reverse3(str + 1);
strncat(dst, str, 1);
return 0;
}边栏推荐
- Hcia-r & s self use notes (20) VLAN comprehensive experiment, GVRP
- Vit:vision transformer super detailed with code
- [untitled]
- Arduino experiment I: two color lamp experiment
- Hcia-r & s self use notes (19) VLAN configuration and experiment, routing between VLANs
- New thrust of Moore's law, detailed explanation of Intel Advanced Packaging Technology!
- 基本的SELECT语句
- 企业如何缓解物联网和工业物联网安全风险
- How can enterprises mitigate the security risks of Internet of things and industrial Internet of things
- 比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
猜你喜欢

Disk expansion process and problems encountered in the virtual machine created by VMWare

【flask高级】结合源码分析flask中的线程隔离机制

MySQL random paging to get non duplicate data

Three person management of system design

Arduino experiment I: two color lamp experiment

kalibr标定realsenseD435i --多相机标定

基本的SELECT语句

Vit:vision transformer super detailed with code

App information reconnaissance & night God simulator burp packet capture configuration

An online accident, I suddenly realized the essence of asynchrony
随机推荐
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“ )
New employees of black maredge takeout
Eureka basic use
Incremental secure file system SFS based on C language design
HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
杰理下载器强制下载工具的使用介绍_AC695N696NAD14AD15全系列支持
比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
Application of workflow engine in vivo marketing automation | engine 03
[shader realizes shine effect _shader effect Chapter 3]
[untitled]
Reduce power consumption and upgrade functions! Qiyingtailun released the second generation voice AI chip: the module price is as low as 14.99 yuan!
An online accident, I suddenly realized the essence of asynchrony
公有云安全性和合规性方面的考虑事项
Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
沟通中经常用到的几个库存术语
华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
Three person management of system design
Typescript stage learning
菜鸟网络面试【杭州多测师】【杭州多测师_王sir】
Do you know the common core types of magnetic ring inductors?