1 / 13

湘南工科大学情報工学科 准教授 小林 学

湘南工科大学. 2013 年 12 月 10 日. 情報 理論2. 湘南工科大学情報工学科 准教授 小林 学. 関数. 受け取る値を入れる変数. 関数が出力する値. P. 2. 引数 ( ひきすう ) : 関数が受け取る値 戻り値 ( もどりち ) : 関数が出力する値. 関数を使うときの書き方. 戻り値の型  関数名( 引数の型 引数 ,.., 引数の型 引数 ) {   関数の本体 return( 戻り値 ); }. 関数の使用例. 小林ルール:   関数名の単語の先頭を大文字にする. x に 3 が入り, y に 5 が入る.

dana
Download Presentation

湘南工科大学情報工学科 准教授 小林 学

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 湘南工科大学 2013年12月10日 情報理論2 湘南工科大学情報工学科 准教授 小林 学

  2. 関数 受け取る値を入れる変数 関数が出力する値 P.2 引数(ひきすう):関数が受け取る値 戻り値(もどりち):関数が出力する値 関数を使うときの書き方 戻り値の型 関数名(引数の型 引数,..,引数の型 引数){   関数の本体 return(戻り値); }

  3. 関数の使用例 小林ルール:   関数名の単語の先頭を大文字にする x に 3が入り, y に 5 が入る 戻り値ansの型 intFunc(int x, int y){ intans; ans = x + y; return(ans); } void main(void){ int a; a = Func(3, 5); printf("%d\n",a); } ansの値が a に入る 実行結果

  4. P.4 [例題1] 二つの整数 x, y の大きい方の値を計算する関数 #include<stdio.h> intMax2(int x, int y){ intans; if(x>y) ans = x; else ans = y; return(ans); } void main(void){ int a; a = Max2(3, 5); printf("最大値:%d\n", a); } 実行結果

  5. P.5 [例題2] 実数 x が0以上ならば1を,xが負ならば-1を返す関数 Sign #include<stdio.h> intSign(double x){ if(x>=0) return(1); return(-1); } void main(void){ int a; a = Sign(-3.7); printf("符号:%d\n", a); } 実行結果

  6. Page 6 [紙に書く課題1] 以下のプログラムの画面出力を用紙に書きなさい

  7. グローバル変数 #include<stdio.h> intDat[5]={0,1,0,0}; //グローバル変数 intACount(void){ inti, count=0; //ローカル変数 for(i=0;i<4;i++){ if(Dat[i]==0) count++; } return(count); } void main(void){ intans; //ローカル変数 ans= ACount(); printf(“0の数:%d\n", ans); } グローバル変数はどの関数でも使えて中身は一つ 小林ルール: グローバル変数の名前の先頭を大文字にする ローカル変数はその関数でしか使えない。別の関数では別の中身

  8. Page 8 [紙に書く課題2] 以下のプログラムの画面出力を用紙に書きなさい

  9. P.9 [プログラミング課題1] 1から n までの和を計算する関数SumNを作成しなさい #include<stdio.h> ???SumN(???){ ??? } void main(void){ int a; a = SumN(10); printf("1から10までの和:%d\n", a); } 実行結果

  10. P.10 [プログラミング課題2] xのy乗を計算して返す関数Bekiを作成しなさい #include<stdio.h> ???Beki(???){ ??? } void main(void){ int a; a = Beki(2 , 3); printf("2の3乗は:%d\n", a); } 実行結果

  11. Page 11 [プログラミング課題3]配列Dat1とDat2の先頭からの連続一致文字数を計算する関数Icchichoのプログラムを作成しなさい #include<stdio.h> intDat1[5]={0,1,1,0}, Dat2[5]={0,1,0,1}; ??? Icchicho(???){ ??? } void main(void){ intans; ans = Icchicho(); printf("先頭から連続一致した文字の数:%d\n", ans); }

  12. Page 12 [プログラミング課題4] 配列Datを左に n シフトする関数Shiftのプログラムを作成しなさい #include<stdio.h> char Dat[10]={0,1,1,0,1,0,1,0,1,-1}; void Shift(???){ //戻り値が無い場合にはvoid ??? } void main(void){ inti; printf("もとのDat:"); for(i=0;i<10;i++) printf("%d,", Dat[i]); Shift(3); printf("\nShift後のDat:"); for(i=0;i<10;i++) printf("%d,", Dat[i]); }

  13. Page 13 [プログラミング課題5] 10進数の数字を2進数に変換する関数を作成しなさい. (配列 Dat [i]に2進数の下から iビット目を入れる) #include<stdio.h> intDat[32]; void Binary(???){ ??? } void main(void){ inti; Binary(100); for(i=31;i>=0;i--) printf("%d", Dat[i]); }

More Related