当前位置:网站首页>函数(二)
函数(二)
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;
}递归总结:
边栏推荐
猜你喜欢

Remove 360's detection and modification of the default browser

Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"

写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用

SIPp installation and use
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it

【无标题】

9月备考PMP,应该从哪里备考?

LTE时域、频域资源

What is the difference between a utility model patent and an invention patent?Understand in seconds!

【多任务优化】DWA、DTP、Gradnorm(CVPR 2019、ECCV 2018、 ICML 2018)
随机推荐
The configuration manual for the secondary development of the XE training system of the missing moment document system
基于FPGA的任意字节数(单字节、多字节)的串口(UART)发送(含源码工程)
MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
Determine a binary tree given inorder traversal and another traversal method
数据库内核面试中我不会的问题(1)
【nn.Parameter()】生成和为什么要初始化
外骨骼机器人(七):标准步态数据库
OSG Notes: Set DO_NOT_COMPUTE_NEAR_FAR to manually calculate far and near planes
KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
98.嵌入式控制器EC实战 EC开发板开发完成
Go Atomic
小数据如何学习?吉大最新《小数据学习》综述,26页pdf涵盖269页文献阐述小数据学习理论、方法与应用
Pytorch框架学习记录9——非线性激活
SIPp installation and use
密码学的基础:X.690和对应的BER CER DER编码
LinkedList源码分享
技能大赛训练:A部分加固题目
Interview assault 70: what is the glue bag and a bag?How to solve?
【节能学院】安科瑞餐饮油烟监测云平台助力大气污染攻坚战
[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)