輸出結(jié)果錯(cuò)誤
?#include?<stdio.h>? /*? ?*?請(qǐng)使用遞歸函數(shù)完成本題 ?*?小編已將正確代碼放在左側(cè)任務(wù)的“不知道怎么辦”里 ?*?小編希望各位童鞋獨(dú)立完成哦~ ?*/ int?getAge(numPeople) { ????int?age; ????if(numPeople==1) ????age=10; ????else ????age=getAge(numPeople-1)+2; ????return?getAge; } int?main()? { ????int?fifthAge=getAge(5); ?printf("第5個(gè)人的年齡是%d歲",fifthAge);? ?return?0; }
2016-12-20
返回值錯(cuò)了,return getAge改為return age就對(duì)了,因?yàn)槟惴祷氐闹凳嵌x在局部函數(shù)中的age變量,而不是外部的函數(shù)。