当前位置:网站首页>函数(二)
函数(二)
2022-08-01 20:50:00 【我可是ikun啊】
紧接上文,我们把函数的基础语法结束了,本章将讲解到较为难一点的内容,譬如递归调用。
一:函数的嵌套调用和链式访问
函数与函数之间并不是独立存在的,函数和函数之间可以根据实际的需求进行组合的,也就是互相调用的。
嵌套调用
举例:
#include <stdio.h>
void new_line()
{
printf("hehe\n");
}
void three_line()
{
int i = 0;
for(i=0; i<3; i++)
{
new_line();
}
}
int main()
{
three_line();
return 0;
}
函数可以嵌套调用,但是不能嵌套定义。
链式访问
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "hello";
int ret = strlen(strcat(arr,"bit"));
printf("%d\n", ret);
return 0;
}
简单介绍一个strlen函数,它被包含在头文件<string.h>中,其作用的是求字符串的长度。
strlen和sizeof的比较:
strlen是一个库函数,在求字符串长度时不计算\0,只计算到\0之前的一个字符。
sizeof是一个操作符,它在求字符串长度时会计算\0。
例二:
#include <stdio.h>
int main()
{
printf("%d", printf("%d", printf("%d", 43)));
return 0;
}
这些都叫做链式访问。
二:函数的声明和定义
函数的声明:
在之前写的函数代码中,很容易看见我们一般都是把函数写在main函数之前,因为我们的编译器在读取代码时都是以行行读取的。在我们的main函数读取到我们自定义函数时,如果自定义函数写在main之前,编译器不会报错;但是自定义函数在main之前,编译器很有可能弹出警报,函数未定义。
注意事项
我们之前写的文件都是在同一个文件中的,那么在不同文件进行调用过程中需要进行引头文件,所以函数声明需要放在头文件中。
函数定义
#ifndef __TEST_H__#define __TEST_H__//函数的声明int Add(int x, int y);#endif //__TEST_H__
#include "test.h"//函数Add的实现int Add(int x, int y){return x+y;}
三:函数递归
什么是递归
递归必须满足的两个必要条件:
接受一个整型值(无符号),按照顺序打印它的每一位。
例如:
输入:1234,输出 1 2 3 4
#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;
}
print传入1234进入函数,在第一个print函数中n>9,再一次进入print函数,此时n的值为123;此时n的值又>9,再一次进入print函数中如此反复,直到n<9从最后一次print函数跳出print函数至上一层的print中。
画图说明:
四:递归与迭代
举例:
例:求第n个斐波那契数。(不考虑溢出)
int fib(int n) {
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int count = 0;//全局变量
int fib(int n) {
if(n == 3)
count++;
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
仅仅只是n=3就计算了非常多次,最后算出来的count结果非常的大。
//求n的阶乘
int factorial(int n)
{
int result = 1;
while (n > 1)
{
result *= n ;
n -= 1;
}
return result;
}
递归总结:
边栏推荐
猜你喜欢
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
4.1 配置Mysql与注册登录模块
【无标题】
[Energy Conservation Institute] Comparative analysis of smart small busbar and column head cabinet solutions in data room
Application of Acrel-5010 online monitoring system for key energy consumption unit energy consumption in Hunan Sanli Group
【Dart】dart之mixin探究
【torch】张量乘法:matmul,einsum
Pytorch框架学习记录8——最大池化的使用
[Multi-task optimization] DWA, DTP, Gradnorm (CVPR 2019, ECCV 2018, ICML 2018)
随机推荐
30+的女性测试人面试经验分享
tiup mirror genkey
Buttons with good user experience should not have hover state on mobile phones
Pytorch框架学习记录13——利用GPU训练
不同的操作加不同的锁详解
【Untitled】
模板特例化和常用用法
【多任务优化】DWA、DTP、Gradnorm(CVPR 2019、ECCV 2018、 ICML 2018)
基于FPGA的任意字节数(单字节、多字节)的串口(UART)发送(含源码工程)
Goroutine Leaks - The Forgotten Sender
OSG笔记:设置DO_NOT_COMPUTE_NEAR_FAR,手动计算远近平面
Redis 做网页UV统计
Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
有用的网站
根据Uniprot ID/PDB ID批处理获取蛋白质.pdb文件
iptables的使用简单测试
织梦通过数据库查询调用当前文章的留言
MongoDB快速上手
Get started quickly with MongoDB
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换