300 likes | 456 Views
アルゴリズムとデータ構造 補足資料 3-2 「関数 fact の再帰呼出」. 横浜国立大学 理工 学部 数物・電子情報系学科 富井尚志. 戻り値が int 型の関数 fact. 仮引数 n. int 型 (32bit ). int fact ( int n ) { int fn; printf (“Call :fact(%d)<br>”, n); if(n>0) fn=n* fact (n-1); else fn=1; printf (“ Return:fact (%d)==%d n”,n,fn );
E N D
アルゴリズムとデータ構造補足資料3-2「関数factの再帰呼出」アルゴリズムとデータ構造補足資料3-2「関数factの再帰呼出」 横浜国立大学 理工学部 数物・電子情報系学科 富井尚志
戻り値がint型の関数 fact 仮引数 n int型(32bit) intfact(int n) { int fn; printf(“Call :fact(%d)\n”, n); if(n>0) fn=n*fact(n-1); else fn=1; printf(“Return:fact(%d)==%d\n”,n,fn); return fn; } (自動)変数 fn int型(32bit) printf(“Call :fact(%d)\n”, n); no yes n>0 ? fn=1; fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn); return int型(32bit)
戻り値がint型の関数 main 仮引数: なし void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, k, fact(k)); return int型(32bit)
戻り値がint型の関数 main 仮引数: なし mainから実行開始! void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, fact(3)); fact(3)を呼び出し return int型(32bit)
fact(3) n 3 fn printf(“Call :fact(%d)\n”, n); no n>0 ? yes fn=1; fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) return
fact(3) n 3 fn printf(“Call :fact(%d)\n”, 3); no 3>0 ? yes fn=1; fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) return
fact(3) n 3 fn printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=3*fact(3-1); printf(“Return:fact(%d)==%d\n”,n,fn) return
fact(3) fact(2) n n 2 3 fn fn printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, n); fact(2) no 3>0 ? n>0 ? yes yes fn=1; fn=3*fact(2); fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return
fact(3) fact(2) n n 2 3 fn fn printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); fact(2) no 3>0 ? 2>0 ? yes yes fn=1; fn=3*fact(2); fn=2*fact(2-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return
fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, n); printf(“Call :fact(%d)\n”, 3); fact(2) no 2>0 ? 3>0 ? n>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=2*fact(1); fn=n*fact(n-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return
fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); fact(2) no 2>0 ? 3>0 ? 1>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=2*fact(1); fn=1*fact(1-1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return
fact(3) fact(0) fact(2) fact(1) n n n n 2 3 0 1 fn fn fn fn printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, n); printf(“Call :fact(%d)\n”, 2); fact(2) no 2>0 ? 1>0 ? n>0 ? 3>0 ? yes yes yes yes fact(1) fn=1; fn=3*fact(2); fn=n*fact(n-1); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return fact(0)
fact(3) fact(0) fact(2) fact(1) n n n n 2 3 0 1 fn fn fn fn printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); printf(“Call :fact(%d)\n”, 2); fact(2) no 2>0 ? 1>0 ? 0>0 ? 3>0 ? yes yes yes yes fact(1) fn=1; fn=3*fact(2); fn=n*fact(n-1); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return fact(0)
fact(3) fact(0) fact(1) fact(2) n n n n 3 0 1 2 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 0); printf(“Call :fact(%d)\n”, 1); fact(2) no 0>0 ? 1>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return fact(0)
fact(3) fact(0) fact(2) fact(1) n n n n 2 0 1 3 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); printf(“Call :fact(%d)\n”, 1); fact(2) no 3>0 ? 1>0 ? 0>0 ? 2>0 ? yes yes yes fact(1) fn=1; fn=3*fact(2); fn=1*fact(0); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,0,1) return return return return 1 fact(0)
fact(3) fact(0) fact(2) fact(1) n n n n 0 3 2 1 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); fact(2) no 1>0 ? 0>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn=1; fn=1*fact(0); fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,0,1) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return 1 fact(0)=1
fact(3) fact(0) fact(2) fact(1) n n n n 0 3 2 1 fn fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 0); fact(2) no 1>0 ? 0>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn=1; fn=1* 1; fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,0,1) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return return 1 fact(0)=1
fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn 1 printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 3); fact(2) 2>0 ? 3>0 ? 1>0 ? yes yes yes fact(1) fn= 1; fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return return
fact(3) fact(1) fact(2) n n n 1 3 2 fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); fact(2) 1>0 ? 2>0 ? 3>0 ? yes yes yes fact(1) fn= 1; fn=3*fact(2); fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,1,1) printf(“Return:fact(%d)==%d\n”,n,fn) return return return 1
fact(3) fact(1) fact(2) n n n 2 3 1 fn fn fn 1 printf(“Call :fact(%d)\n”, 1); printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); fact(2) 1>0 ? 2>0 ? 3>0 ? yes yes yes fn=3*fact(2); fn= 1; fn=2*fact(1); printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,1,1) printf(“Return:fact(%d)==%d\n”,n,fn) fact(1)=1 return return return 1
fact(3) fact(2) n n 3 2 fn fn 2 printf(“Call :fact(%d)\n”, 2); printf(“Call :fact(%d)\n”, 3); fact(2) 2>0 ? 3>0 ? yes yes fn=3*fact(2); fn=2; printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,n,fn) return return
fact(3) fact(2) n n 3 2 fn fn 2 printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); fact(2) 3>0 ? 2>0 ? yes yes fn=3*fact(2); fn=2; printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,2,2) return return 2
fact(3) fact(2) n n 2 3 fn fn 2 printf(“Call :fact(%d)\n”, 3); printf(“Call :fact(%d)\n”, 2); 2>0 ? 3>0 ? yes yes fn=2; fn=3*fact(2); fact(2)=2 printf(“Return:fact(%d)==%d\n”,n,fn) printf(“Return:fact(%d)==%d\n”,2,2) return return 2
fact(3) n 3 fn printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=3*2; printf(“Return:fact(%d)==%d\n”,n,fn) return
fact(3) n 3 fn 6 printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=6; printf(“Return:fact(%d)==%d\n”,n,fn) return
fact(3) n 3 fn 6 printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=6; printf(“Return:fact(%d)==%d\n”,3,6) return 6
fact(3) n 3 fn 6 printf(“Call :fact(%d)\n”, 3); 3>0 ? yes fn=6; printf(“Return:fact(%d)==%d\n”,3,6) return 6 main関数へfact(3)=6
戻り値がint型の関数 main 仮引数: なし main関数に戻る void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, fact(3)); fact(3)を呼び出し fact関数からfact(3)=6 return int型(32bit)
戻り値がint型の関数 main 仮引数: なし main関数に戻る void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, 6); fact関数からfact(3)=6 return int型(32bit)
戻り値がint型の関数 main 仮引数: なし void 型(0bit) (自動)変数 k 3 intmain(void) { int k=3; printf(“fact(%d)=%d\n”, k, fact(k)); return 0; } int型(32bit) printf(“fact(%d)=%d\n”, 3, 6); return 0 正常終了 int型(32bit)