この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
wiki:defold:tips:008 [2016/04/30 13:37] step |
wiki:defold:tips:008 [2016/04/30 14:01] (現在) step |
||
---|---|---|---|
ライン 108: | ライン 108: | ||
put into the string and is sometimes very handy. | put into the string and is sometimes very handy. | ||
]] | ]] | ||
- | |||
</sxh> | </sxh> | ||
==== 関数 ==== | ==== 関数 ==== | ||
+ | <sxh c++; title:> | ||
+ | -- MyPlusという名の関数で、 a + b の結果を返す | ||
+ | MyPlus = function(a, b) | ||
+ | return a + b | ||
+ | end | ||
+ | -- MyPlusという名の関数で、 a * b の結果を返す | ||
+ | function MyMultiple(a, b) | ||
+ | return a * b | ||
+ | end | ||
+ | |||
+ | -- 関数を引数に渡すことができる | ||
+ | function operate(func, a, b) | ||
+ | return func(a, b) | ||
+ | end | ||
+ | -- 使用例 | ||
+ | print(operate(MyPlus, 4, 5)) --> 9 | ||
+ | |||
+ | |||
+ | -- Create an adder function and return it | ||
+ | function create_adder(n) | ||
+ | return function(a) | ||
+ | return a + n | ||
+ | end | ||
+ | end | ||
+ | |||
+ | adder = create_adder(2) -- n に2が入った状態で初期化。 | ||
+ | print(adder(3)) --> 2+3 = 5 | ||
+ | print(adder(10)) --> 2+10 = 12 | ||
+ | </sxh> | ||
- | 近日記述予定 |