C++ 函数的嵌套调用

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;
    
}








 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值