I 2010 0603 http www ns kogakuin ac
- Slides: 41
プログラミング論 I 2010年 06月03日 関数 http: //www. ns. kogakuin. ac. jp/~ct 13140/Prog. 2010/ E-1
関数の概要 1/3 1 #include <stdio. h> 2 void main(){ この 3行まとめて, 3 printf("Hello, "); 1個のグループに 4 printf("World"); したい. 5 printf("!!!!n"); 6 printf("------n"); 7 printf("Hello, "); 8 printf("World"); 9 printf("!!!!n"); 10 printf("######n"); 11 printf("Hello, "); Hello, World!!!! 12 printf("World"); 実 ------13 printf("!!!!n"); 行 結 Hello, World!!!! 14 } 果 ###### Hello, World!!!! E-6
関数の動作 0/2 ① 3 void print. HW(){ ④ 4 printf("Hello, "); 5 printf("World"); 6 printf("!!!!n"); 7⑤} 9 void main(){ ③ ② 10 print. HW(); ⑥ 11 printf("------n"); 12 print. HW(); 13 printf("######n"); 14 print. HW(); 15 } E-8
関数の動作 1/2 3 void print. HW(){ ⑨ 4 printf("Hello, "); 5 printf("World"); 6 printf("!!!!n"); 7⑩} 9 void main(){ 10 print. HW(); ⑦ printf("------n"); 11 ⑧ 12 print. HW(); ⑪ 13 printf("######n"); 14 print. HW(); 15 } E-9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <stdio. h> void sankaku(){ printf("*n"); printf("***n"); printf("****n"); } void gyaku_sankaku(){ printf("****n"); printf("**n"); printf("*n"); } void main(){ sankaku(); gyaku_sankaku(); } 関数の作り方 (例) この 1行で, sankaku() の 全てが一括して 実行される. 実 行 結 果 * ** **** *** ** *** **** *** ** * E-13
解答 (い) #include <stdio. h> void pr_hl_wld(){ printf("Hello, n"); printf("World!n"); } void main(){ pr_hl_wld(); } E-15
関数の作り方 (引数の例) #include <stdio. h> void print. HW_n(int n){ int i; printf("Hello, World"); for(i=0; i<n; i++){ printf("!"); } /* n個の ! を表示 */ printf("n"); } void main(){ print. HW_n(4); print. HW_n(7); } for文を使う例 実行結果 Hello, World!!!!!!! E-22
関数の作り方 (戻り値の例) 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio. h> int square(int n){ int sq; sq = n*n; printf("%d^2 = %dn", n, sq); return sq; } void main(){ int s; s = square(5); printf("square=%dn", s); } 実行結果 5^2 = 25 square=25 E-30
関数の作り方 (戻り値の例) 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio. h> double_square(double d){ 引数がdouble型1個なら, double sq; double型の値 1個を sq = d*d; 入れて,呼び出す. printf("%lf^2 = %lfn", d, sq); return sq; } 実行結果 1. 500000^2 = 2. 250000 void main(){ square=2. 250000 double s; s = double_square(1. 5); printf("square=%lfn", s); } E-31
関数の作り方 (戻り値の例) 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio. h> double_square(double d){ 戻り値がdouble型なら, double sq; double型の値を sq = d*d; return する. printf("%lf^2 = %lfn", d, sq); return sq; } 実行結果 1. 500000^2 = 2. 250000 void main(){ square=2. 250000 double s; s = double_square(1. 5); printf("square=%lfn", s); } E-32
関数の作り方 (戻り値の例) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio. h> int one(){ return 1; } int two(){ return 2; } int three(){ return 3; } i=1, void main(){ int i, j, k; i = one(); j = two(); k = three(); printf("i=%d, j=%d, k=%dn", i, j, k); } 実行結果 j=2, k=3 E-33
関数の作り方 (戻り値の例) 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio. h> int one(){ return 1; } int two(){ return 2; } int three(){ 実行結果 return 3; } i=1, j=2, k=3 void main(){ printf("i=%d, j=%d, k=%dn", one(), two(), three()); } printf()の中に直接記述してもOK. one() が"1",two()が"2"と思えば良い. E-34
関数の作り方 (戻り値がない例) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio. h> void one(){ printf("1n"); } void two(){ printf("2n"); } void three(){ printf("3n"); } void main(){ int i, j, k; one(); two(); three(); } 実行結果 1 2 3 E-35
関数:複数のreturn • 関数内に return が 何個あってもよい. 実行結果 x=3 |x|=3 x=-4 |x|=4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio. h> int zettaichi(int n){ if( n<0 ){ return (-n); } else { return n; } } void main(){ int x, z; x = 3; z = zettaichi(x); printf("x=%d |x|=%dn", x, z); x = -4; z = zettaichi(x); printf("x=%d |x|=%dn", x, z); } E-38
関数:return • return により, 関数は強制的に 終了する. 実行結果 AAAA BBBB 戻り値が無いときは, 単に return; と記述 1 2 3 4 5 6 7 8 9 10 11 12 #include <stdio. h> void funct(){ printf("AAAAn"); printf("BBBBn"); return; printf("CCCCn"); } void main(){ funct(); } E-39
- Napkollektor feltöltése
- Protective leaf like enclosure for the flower bud
- T. trimpe 2010 http://sciencespot.net/
- Http //mbs.meb.gov.tr/ http //www.alantercihleri.com
- Siat.ung.ac.id krs
- Surat panggilan menghadap
- 54 tahun 2010
- Abnt nbr iso 26000:2010
- Ms word 2007 view tab
- Environmental permitting regulations 2010
- August 26 2010
- Rd 455/2010
- Copyright 2010 pearson education inc
- June 2010 chemistry regents
- National lottery funding application form 2010/1
- 2010 pearson education inc
- Global earthing system
- Copyright 2010
- 2010 pearson education inc
- Da toolset
- Mul 2010 ucf
- Rda 2010
- Copyright 2010 pearson education inc
- Biology 30 diploma 2005
- Slide todoc.com
- Censo de poblacion 2010
- 20/10/2010
- 2010 pearson education inc answers
- Microsoft visual c express 2010
- Calendario escolar 2009 a 2010 sep
- Aha guidelines 2010
- Education and training 2010
- 2010 pearson education inc
- Barra de word y sus partes
- Iter segnalazione operazione sospetta schema
- Replay queue length exchange 2010
- What is syntax
- Robin hood cda
- 2010 pearson education inc
- Monthly planner 2010
- Unidroit principles 2010
- Hurricane karl 2010