Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
変数と関数 | 夢を叶える塾
スポンサーリンク

変数と関数

ホームページのリニューアルに伴い一部のページは新しいサイトにリダイレクトされます。

 

連載も5話目です。
少し復習しておきたいと思います。

少しだけ復習

ここまでに、

  1. プログラムは「パソコンに演算や画面操作を行う支持を与えるプログラム言語に従った命令文の集合」
  2. マスターまでの道のりはそれなりに長いものの、最初の一歩が踏み出せればプログラミングの基礎習得は容易い
  3. アルゴリズムは、「演算」、「入出力」、「条件分岐」、「反復」等の組み合わせ
  4. プログラムを実行するまでのステップは意外と長い?

と言った話をさせていただきました。
プログラミングはとっつきにくい面があると思っていますが、最初の一歩を進めるまでの意欲があればその後は意外と楽です。
「やるの面倒だな」と思われた方は、やり出すともっと大変なのでやめておいた方が良いと思います。
たぶんプログラムができなくても生きていけますので、他の事に時間を使われた方が有意義だと思います。
「意外と簡単そうだな」と思われた方は、やり出すと意外と簡単なのでこのまま進めて頂けると良いと思います。
そういうところをお伝えしたくここまで進めてきました。

必要不可欠な変数と関数

ここからは実際にプログラムでどうやってやりたいことをやれるようになるのかに進んでいきたいと思います。
今回はプログラミングに必要不可欠な変数と関数の二つに絞ってお話しします。

変数

まず変数についてです。
簡単に言えば、中学1年生の数学で習う「文字式」です。

プログラムの中では必ずと言っていいほど「数」や「文字列」をプログラムに覚えさせなければいけません。
数当てゲームでいえば「正解の数」や「入力された数」をプログラムが覚えなければならないですね。
覚えると同時に、条件分岐の中で「正解の数と入力された数が等しい」事を判断するためには、その「正解の数」や「入力された数」をすぐに思い出さなければなりません。
例えば「正解の数」を「21」、「入力された数」を「25」とパソコンに覚えてもらうと、「正解の数」と名付けられた数をプログラムの中で使えるようになります。
この「正解の数」や「入力された数」が変数です。
日本語の意味で「正解の数」は正解の数ですが、プログラムにとっては「正解の数」と呼ばれる値に過ぎません。
変数に元々意味はなく、意味を持たせるのはプログラムの製作者です。
また、プログラムは基本的に日本語で記述できませんので、アルファベットを使います。
「「seikai」という文字列を「正解の数」として扱う」という事をプログラムの製作者が決めてプログラムの中で使っていきます。

説明の中で「数」ばかり扱いましたが、「文字列」も変数に覚えさせることができます。
「正解の文字列」である「seikai」に「セイカイ」という文字列を覚えさせる事もできます。

代入と参照

変数の使い方は主に2種です。

一つは代入です。
「正解の数」を「21」と覚えさせることが代入です。
これは「seikai=21」等とプログラムの中で記述します。
これでプログラムは「seikai」は「21」という数であることを覚えます。

もう一つは参照です。
「正解の数」に1を足した数を計算する時、「seikai+1」等とプログラムの中で記述します。
先の「seikai=21」として「seikai+1」を表示させれば「22」になります。

これを組合せるとこんな記述になります。
「seikai=seikai+1」
「正解の数」に「正解の数に1足した数」を代入しています。
元々「seikai」が「21」であれば、この命令により「seikai」は「22」になります。

中学生で習う方程式の様に「=」をとらえてしまうと、「seikai=seikai+1」は正しくない式です。
しかしプログラムの世界では常識的に使われています。

配列

2話目のプログラミングをマスターするまでの話の中で、衝撃を受けた「配列」の話をしました。

変数の弱点としては、あらかじめその箱となる変数に名前を付けておかなければなりません。
例えばこんなプログラムです。

  1. 入力する回数を決める
  2. 入力する回数が1回以上であれば、「数1」を入力する
  3. 入力する回数が2回以上であれば、「数2」を入力する
  4. 入力する回数が3回以上であれば、「数3」を入力する
  5. 入力する回数が4回以上であれば、「数4」を入力する
  6. ・・・

これは「数99」まで用意しても入力する回数が100であれば意味が無くなります。
変数の数が決まっていない場合、どこまで変数を用意しておけばよいのか、困ってしまいます。
可変的な要素があるとプログラムが対応できなくなるという事です。

これを解決してくれるのが「配列」です。
配列は「数(n)」という形の変数です。
「n」には自然数が入り、そのnを変化させて変数の数を増減させることができます。
※プログラム言語によって0を含みますが、ここではわかりやすさ重視で1からにしています
「数1」に「数(1)」を、「数2」に「数(2)」を対応させるという事ができるようになります。

実際にプログラムを見てみましょう。

  1. 入力する回数nを決める
  2. 数(n)を用意する
  3. m=1とする
  4. 5~6をn回繰り返す
  5. 「数(m)」を入力する
  6. m=m+1

これで最初は「数(1)」に数を代入し、二回目は「m=1+1」で「m=2」になりますから「数(2)」に数を代入する事になります。
nの値がいくつであってもプログラムを実行し、想定通りの処理を行うことができます。

関数

次に関数についてです。
変数と同じように関数も無くてはならない存在です。
これも中学1年生の数学で習う「関数」と同じです。

「yがxの関数である」とは「xの値を決めるとそれに対応するyの値が1つだけ決まる」ことを言います。
プログラムでも何か1つ(もしくは複数、場合によっては1つもない)の値から、1つ(もしくは複数、場合によっては1つもない)の値を得る計算等のルールを関数として扱います。
ある計算などのルール・・・まさにプログラムです。
ただ文字ではわかりにくいですね。

例えばある計算ルールが「与えられたxに1を足す」というものであればこれは関数になります。
この関数に「1」を与えれば「1+1=2」が得られ、「2」を与えれば「2+1=3」が得られます。
まさに関数「f(x)=x+1」ですね。
あるいは「与えられた2つの数の和を求める」というものも関数です。
この関数に「1」と「2」を与えれば「1+2=3」が得られ、「2」と「-3」を与えれば「2-3=1」が得られます。

文字列でも関数は作ることができます。
例えば「与えられた文字列の空白部分を取り除く」というものも関数です。
この関数に「ア イ ウ エオ」を与えると「アイウエオ」になります。
あるいは「与えられた文字列をさかさまにする」というものも関数です。
この関数に「ア イ ウ エオ」を与えると「オエ ウ イ ア」になります。

引数と戻り値

この関数で与える値を「引数」と言います。
関数によって得られる結果を「戻り値」と言います。

終りに

今回解説しました変数と関数はプログラム独特の考え方になるのではないかと思います。
ここまで変数を使わずにプログラムを説明してきましたが、変数を封じられると説明がかなり難しくなります。

そしてプログラムでは数学で学ぶ考え方が多く取り入れられています。
関数については、プログラミングの知識があると、数学の関数の理解も深まると思います。
今回は説明していませんが「論理演算子」という、条件式の演算も数学で学ぶことのできる考え方です。
そのため、できれば小学生の頃にプログラムを学べると良いと思っていたら、2020年以降実際にそうなる事となりました。
ただ、学校教育の中でプログラムを教えることのできる先生は余りいないでしょうし、とっつきにくいものですから難しいでしょうね。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

にほんブログ村 地域生活(街) 東北ブログへ にほんブログ村 地域生活(街) 東北ブログ 米沢情報へ にほんブログ村 教育ブログへ にほんブログ村 教育ブログ 塾教育へ
スポンサーリンク