利用函式關係做數數字練習(程式執行順序)

#include<stdio.h>
void countto3(int);
void count3to1(int);
void c3to1(int);
int main(){
c3to1(1);
countto3(1);
count3to1(3);
return 0;
}
void countto3(int i){
if(i<=3){
printf("%d\n",i);
countto3(i+1);
}
}
void count3to1(int i){
if(i>=1){
printf("%d ",i);
count3to1(i-1);
}
}
void c3to1(int i){
if(i<=3){
c3to1(i+1);
printf("c3to1:%d\n",i);
}
}

函式有趣的是,如同範例中的c3to1,因為程式有處裡順序,所以在此函式中因為printf是在呼叫之後,所以會先執行呼叫函式當最末函式執行完成後,才會回到前一函式繼續執行且繼續完成。

留言