LoginSignup
0
0

More than 3 years have passed since last update.

JavaScriptでユーザー定義関数の使い方

Last updated at Posted at 2019-10-23

まず、関数とは?

何らかの処理を一つにまとめて、
関数名で呼び出せるようにしたもの

作成方法

funcion 関数名 (引数1,引数2,....)
{
 <処理
 return 戻り値;
}

  • 関数は英語で[function]
  • 引数はカンマで区切る
  • 関数を行った時の結果の戻り値をreturnで指定する
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
...
... 
<title>ユーザー定義関数の作成</title>
<script type="text/javascript">

function priceOfFruit(unitPrice,count){
    var fruit;
    fruit = unitPrice * count;
    return fruit;
}

</script>
</head>

※ユーザー定義関数は好きな所で定義することが出来るけども
head関数に定義することで分かりやすくなる。

...
...

<script type="text/javascript">

    var fruit1 priceOfFruit= (100, 5);
    document.write(fruit1,"個</br>")

</script>
</p>
</body>
</html>
0
0
1

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
0
0