LoginSignup
1
0

More than 5 years have passed since last update.

関数

Last updated at Posted at 2018-07-06
var num = 100;
if(num == 100){
 num = num*1.08;
 alert('変数numは'+num+'です');

}elseif(num == 200){
 num = num*1.08;
 alert('変数numは'+num+'です');

}elseif(num == 300){
 num = num*1.08;
 alert('変数numは'+num+'です');

}

これが、関数を使うとこうなる↓

var num = 100;
if(num == 100){
 alert(total(num));

}elseif(num == 200){
 alert(total(num));

}elseif(num == 300){
 alert(total(num));

}

function total(hikisuu){
 var result = hikisuu * 1.08;
 return '変数numは'+result+'です';
}

同じような処理をまとめることができる。

基本的な形は↓

function 関数名(引数1,引数2,引数3){
 //処理
 return ;
}

引数は、渡したい値を受け取る変数。作った変数名はこの関数内だけで使える。なければ空でも良い。複数の場合はカマで区切る。関数名が同じでも、引数の個数が違うと別物と判断されるので注意。

ウェブカツ!!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0