新人Y子のプログラミングのお部屋11

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]今回は、もう少し実用性のある関数の使い方として、
関数へ「値の引き渡し」方法を見てみましょう。
[/speech_bubble]
[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-hatena.png” name=”新人Y子”]難しくなってきますか?
[/speech_bubble]
[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]いいえ、そんなことはありせん。
以下のソースコードを見てください。
[/speech_bubble]

*「void」は別の機会に解説しますので、今は無視してください。

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]
まず、3行目の、add()関数のカッコ内にある部分に注目してください。
この部分を、関数の「引数」と呼びます。
[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]「引数」 ・・・ インスウ?
[/speech_bubble]
[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]「ひきすう」と読みます。
[/speech_bubble]
[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]この引数を使用して、関数に値を引き渡すのです。
[/speech_bubble]
[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]そういえば、前回の問題は、「引数」が空でしたね。

[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]この例のadd()関数は、「a」と「b」の2つ引数がある状態です。

[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]add()関数に、2つ値を引き渡すという事ですか?
[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]その通りです。
そして、関数呼び出し側がセットした引数の数を確認してください。

呼び出される側と同じ数の引数をセットしていると思います。
[/speech_bubble]
[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]確かに、12行目の呼び出し側は、「-222」と「333」の2つの数字を指定していますね。
[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]ところで、引数の「int a」や「int b」の「int」はどんな意味があるのですか?
[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]引数の型を示しています。
「int」型は、整数型の引数であることを意味します。
[/speech_bubble]
[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]そうなのですね。
[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]5行目の「int c」も引数ですか?

[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]こちらは、引数ではありません。変数といいます。
変数のうち、値を引き渡すものが引数といいます。

[/speech_bubble]
[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-normal.png” name=”新人Y子”]引数は、変数の仲間なのですね。
[/speech_bubble]

[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]はい。
それでは、宿題です。
[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”sanoyu-bikkuri.png” name=”新人Y子”]いきなりですか!
[/speech_bubble]
[speech_bubble type=”std” subtype=”L1″ icon=”itokuma-normal.png” name=”しげちゃん先生”]上のプログラムについて宿題です。
1) 実行した時の出力内容を予想してください。
2) 実際に「goding ground」で実行して出力結果を確認してください。
[/speech_bubble]

[speech_bubble type=”std” subtype=”R1″ icon=”techtrageman-normal.png” name=”テクトレージマン”]次回に続く[/speech_bubble]

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

目次