当前位置:网站首页>函数(二)
函数(二)
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;
}
递归总结:
边栏推荐
- To promote energy conservation institute 】 【 the opinions of the agricultural water price reform
- 【Kaggle】House Prices
- OSG Notes: Set DO_NOT_COMPUTE_NEAR_FAR to manually calculate far and near planes
- Go Atomic
- WhatsApp group sending actual combat sharing - WhatsApp Business API account
- tiup mirror
- Pytorch框架学习记录13——利用GPU训练
- MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
- Pytorch框架学习记录9——非线性激活
- 解除360对默认浏览器的检测与修改
猜你喜欢
Use WeChat official account to send information to designated WeChat users
算法---解码方法(Kotlin)
【Social Media Marketing】How to know if your WhatsApp is blocked?
【节能学院】推进农业水价综合改革的意见解读
WhatsApp group sending actual combat sharing - WhatsApp Business API account
98.嵌入式控制器EC实战 EC开发板开发完成
【节能学院】智能操控装置在高压开关柜的应用
数字孪生北京故宫,元宇宙推进旅游业进程
Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
【无标题】
随机推荐
密码学的基础:X.690和对应的BER CER DER编码
泰德制药董事长郑翔玲荣膺“2022卓越影响力企业家奖” 泰德制药荣获“企业社会责任典范奖”
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换
Go Atomic
Imitation cattle forum project
Failed to re-init queues : Illegal queue capacity setting (abs-capacity=0.6) > (abs-maximum-capacity
数字孪生北京故宫,元宇宙推进旅游业进程
LTE时域、频域资源
人工智能可信安全与评测
WeChat applet cloud development | personal blog applet
【个人作品】无线网络图传模块
【无标题】
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
】 【 nn. The Parameter () to generate and why do you want to initialize
【Kaggle】House Prices
【Untitled】
StringTable Detailed String Pool Performance Tuning String Concatenation