C++ 函数的嵌套调用
2016-03-05 16:01
218 查看
C++ 不允许对函数进行嵌套定义 但是可以进行嵌套调用
#include<iostream>
using namespace std;
/* 递归调用
第一个人年龄age1 年龄十岁
第二个人年龄age2 比第一个人大2岁
第三个人年龄age3 比第二个人大2岁
第四个人年龄age4 比第三个人大3
第五个人年龄age5 比第四个人大2岁
求第五个人年龄
*/
int age_1();
int age_2();
int age_3();
int age_4();
int age_5();
int age_5(){
int age5;
return age5=age_4()+2;
}
int age_4(){
int age4;
return age4=age_3()+3;
}
int age_3(){
int age3;
return age3=age_2()+2;
}
int age_2(){
int age2 ;
return age2=age_1()+2;
}
int age_1(){
int age1;
return age1=10;
}
int main(){
int age1,age2,age3,age4,age5;
age1=age_1() ;
age2=age_2() ;
age3=age_3() ;
age4=age_4() ;
age5=age_5() ;
cout<<"age1="<<age1<<endl;
cout<<"age2="<<age2<<endl;
cout<<"age3="<<age3<<endl;
cout<<"age4="<<age4<<endl;
cout<<"age5="<<age5<<endl;
}
#include<iostream>
using namespace std;
/* 递归调用
第一个人年龄age1 年龄十岁
第二个人年龄age2 比第一个人大2岁
第三个人年龄age3 比第二个人大2岁
第四个人年龄age4 比第三个人大3
第五个人年龄age5 比第四个人大2岁
求第五个人年龄
*/
int age_1();
int age_2();
int age_3();
int age_4();
int age_5();
int age_5(){
int age5;
return age5=age_4()+2;
}
int age_4(){
int age4;
return age4=age_3()+3;
}
int age_3(){
int age3;
return age3=age_2()+2;
}
int age_2(){
int age2 ;
return age2=age_1()+2;
}
int age_1(){
int age1;
return age1=10;
}
int main(){
int age1,age2,age3,age4,age5;
age1=age_1() ;
age2=age_2() ;
age3=age_3() ;
age4=age_4() ;
age5=age_5() ;
cout<<"age1="<<age1<<endl;
cout<<"age2="<<age2<<endl;
cout<<"age3="<<age3<<endl;
cout<<"age4="<<age4<<endl;
cout<<"age5="<<age5<<endl;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 如何组织构建多文件 C 语言程序(二)
- 关于指针的一些事情
- 如何写好 C main 函数
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua和C语言的交互详解
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解