@@ -47,7 +47,7 @@ greet("morning", "佐藤");
4747
4848## <Term type =" javascriptReturnValue " >戻り値</Term >
4949
50- <p ><Term type =" javascriptFunction " >関数</Term >呼び出しは<Term type =" javascriptExpression " >式</Term >の一種です。<Term type =" javascriptFunction " >関数</Term >定義内で <strong >return 文</strong >を用いると、<Term type =" javascriptFunction " >関数</Term >の実行が停止され、<Term type =" javascriptFunction " >関数</Term >呼び出し<Term type =" javascriptExpression " >式</Term >の<Term type =" javascriptEvaluation " >評価</Term >結果が確定します。この値を<Term strong type =" javascriptReturnValue " >戻り値</Term >と呼びます。ある<Term type =" javascriptValue " >値</Term >を<Term type =" javascriptReturnValue " >戻り値</Term >として設定して関数の実行を終了することを 、<Term type =" javascriptFunction " >関数</Term >がその<Term type =" javascriptValue " >値</Term >を<Term strong type =" javascriptReturn " >返す</Term >と表現します。</p >
50+ <p ><Term type =" javascriptFunction " >関数</Term >呼び出しは<Term type =" javascriptExpression " >式</Term >の一種です。<Term type =" javascriptFunction " >関数</Term >定義内で <strong >return 文</strong > を用いると、<Term type =" javascriptFunction " >関数</Term >の実行が停止され、<Term type =" javascriptFunction " >関数</Term >呼び出し<Term type =" javascriptExpression " >式</Term >の<Term type =" javascriptEvaluation " >評価</Term >結果が確定します。この値を<Term strong type =" javascriptReturnValue " >戻り値</Term >と呼びます。ある<Term type =" javascriptValue " >値</Term >を<Term type =" javascriptReturnValue " >戻り値</Term >として設定して< Term type = " javascriptFunction " >関数</ Term >の実行を終了することを 、<Term type =" javascriptFunction " >関数</Term >がその<Term type =" javascriptValue " >値</Term >を<Term strong type =" javascriptReturn " >返す</Term >と表現します。</p >
5151
5252``` javascript
5353function add (a , b ) {
@@ -64,6 +64,24 @@ document.write(add(3, 4));
6464
6565<video src ={returnValueVideo} controls autoPlay muted loop />
6666
67+ ::: tip
68+ ** return 文** が実行された時点で<Term type =" javascriptFunction " >関数</Term >の処理が終了するため、次のように書くことで [ if ~ else 文] ( ../if-statement/#if--else ) や [ && (AND) 演算子] ( ../boolean/#論理演算子 ) の繰り返しを避けつつ、複数の条件のついた処理を実行することができます。
69+
70+ ``` javascript
71+ let age = 21 ;
72+ let hasDriverLicense = true ;
73+ let isDrunk = true ;
74+ function tryToDrive () {
75+ // if 構文で実行する式が一行だけの場合、{} を省略できます。
76+ if (age < 18 ) return ;
77+ if (! hasDriverLicense) return ;
78+ if (isDrunk) return ;
79+ document .write (" 車を運転できます。" );
80+ }
81+ ```
82+
83+ :::
84+
6785## <Term type =" javascriptVariable " >変数</Term >の<Term type =" javascriptScope " >スコープ</Term >
6886
6987<p ><Term type =" javascriptFunction " >関数</Term >内で<Term type =" javascriptDeclaration " >宣言</Term >された<Term type =" javascriptVariable " >変数</Term >は、<Term type =" javascriptFunction " >関数</Term >内でのみ有効です。<Term type =" javascriptVariable " >変数</Term >が有効な範囲のことを、その<Term type =" javascriptVariable " >変数</Term >の<Term type =" javascriptScope " strong >スコープ</Term >と呼んでいます。</p >
@@ -105,23 +123,57 @@ increment();
105123
106124:::
107125
126+ ## モジュール化
127+
128+ 複雑な操作を <Term type =" javascriptFunction " >関数</Term > として <Term strong type =" javascriptModularization " >モジュール化</Term > して複数のブロックに分解することで、コードの可読性を上げることができます。
129+
130+ モジュール化前:
131+
132+ ``` javascript
133+ const stringToRepeat = " ☆" ;
134+ for (let i = 0 ; i < 10 ; i += 1 ) {
135+ let result = " " ;
136+ for (let j = 0 ; j < i; j += 1 ) {
137+ result += stringToRepeat;
138+ }
139+ document .write (result);
140+ document .write (" <br>" );
141+ }
142+ ```
143+
144+ モジュール化後:
145+
146+ ``` javascript
147+ function repeat (stringToRepeat , times ) {
148+ let result = " " ;
149+ for (let j = 0 ; j < times; j += 1 ) {
150+ result += stringToRepeat;
151+ }
152+ return result;
153+ }
154+ for (let i = 0 ; i < 10 ; i += 1 ) {
155+ document .write (repeat (" ☆" , i));
156+ document .write (" <br>" );
157+ }
158+ ```
159+
160+ この例における` repeat ` <Term type =" javascriptFunction " >関数</Term >は、第一<Term type =" javascriptParameter " >引数</Term >の<Term type =" javascriptString " >文字列</Term >を第二<Term type =" javascriptParameter " >引数</Term >回だけ繰り返し<Term type =" javascriptStringConcatenation " >足した</Term >ものを返します。
161+
108162## 演習
109163
110164携帯電話料金を計算する<Term type =" javascriptFunction " >関数</Term >を作ってみましょう。
111165
112166``` javascript
113- function calculateCost (monthlyBandwidth ) {
167+ function calculateCost (monthlyDataUsage ) {
114168 // ここに処理を書く
115169}
116170
117171document .write (calculateCost (3.5 ));
118172```
119173
120- ` calculateCost ` は、<Term type =" javascriptParameter " >引数</Term >に月間転送量 ` monthlyBandwidth ` を取り、その月の携帯電話料金を<Term type =" javascriptReturnValue " >戻り値</Term >として<Term type =" javascriptReturn " >返す</Term ><Term type =" javascriptFunction " >関数</Term >です。携帯電話料金は、下のルールで決定されるとします。
174+ ` calculateCost ` は、<Term type =" javascriptParameter " >引数</Term >に月間転送量 ` monthlyDataUsage ` を取り、その月の携帯電話料金を<Term type =" javascriptReturnValue " >戻り値</Term >として<Term type =" javascriptReturn " >返す</Term ><Term type =" javascriptFunction " >関数</Term >です。携帯電話料金は、下のルールで決定されるとします。
121175
122- > 月間転送量を _ monthlyBandwidth_ (GB) とします。
123- >
124- > - _ monthlyBandwidth_ < 5.0 のとき、携帯電話料金は _ monthlyBandwidth_ × 600 (円)
125- > - _ monthlyBandwidth_ >= 5.0 のとき、携帯電話料金は 3000 (円)
176+ > - 月間転送量 < 5.0 (GB) のとき、携帯電話料金は 月間転送量 × 600 (円/GB)
177+ > - 月間転送量 >= 5.0 (GB) のとき、携帯電話料金は 3000 (円)
126178
127179<ViewSource url ={import.meta.url} path =" _samples/mobile-phone-bill " />
0 commit comments