BasicsTutorial/Functions

Last-modified: 2009-08-26 (水) 14:26:08

Funtions

関数

Functions are a very handy part of coding, they allow you to completely separate off parts of your code so they are like a completely different program.
関数はコーディングの非常に便利な部分である、それは完全にあなたのコードの部分を分けることができるので、それは完全に異なるプログラムのようである。
They have their own variables, labels, arrays etc.
それは、それ自身の変数、ラベル、配列 その他を持つ。
Here is how to form them.
ここでは、作る方法は、それらである。

`Store a string in a variable『文字を変数に保管しなさい
stringVar$="This is a variable outside a function"
printText()
print stringVar$
wait key
end
function printText()
  `Store a string in a variable with the same name, except inside a function
  ` 『関数の中に以外、同じ名前でストリングを変数に保管しなさい
  stringVar$="This is a variable inside a function"
  print stringVar$
endfunction

The variables are two completely different variables despite having exactly the same names.
変数は、正確に同じ名前を持つことにもかかわらず 2つの完全に異なる変数である。

You start a function with FUNCTION then you write the name of the function followed by brackets.
あなたは関数を FUNCTIONで始める、そして、あなたは括弧が続く関数の名前を書く。
When you need to end the function you need the command ENDFUNCTION.
あなたが関数を終える必要があるとき、あなたは命令 ENDFUNCTIONを必要とする。
You probably notice the similarities between functions and sub-routines.
あなたは、多分 関数とサブルーチンとの類似点に気がつくだろう。
The difference is that sub-routines just divide up your code into visibly separate parts, whereas functions actually divide it up into physically separate parts.
違いは サブルーチンがちょうど目にみえて別々の部品にあなたのコードを分割するということであるのに、関数は身体的に別々の部品に実際にそれを分割する。

We call the function by typing its name.
我々は、その名前を入力することによって、関数を呼ぶ。
You may notice that it looks exactly like a command.
あなたは、それが正確に命令のように見えると気がつくかもしれない。
Well it is!
よく、それはそうである!
Functions are commands, just ones in code.
関数は、命令である。(ちょうどコードのもの)
You may be thinking “If these are commands, then you must be able to pass values in and get values out”.
あなたは、「これらが 命令であるならば、あなたは値を渡して、値を取り出すことができるにちがいない」と思っているかもしれない。
Well you can!
よく、あなたはそうすることができる!

added=addNumbers(4,5)
print added
print addNumbers(2,2)
wait key
end
function addNumbers(num1,num2)
  added=num1+num2
endfunction added

To have values that you can input into your functions you need to put them inside the brackets in your function statements.
あなたがあなたの関数に入力することができる値を持つために、あなたはあなたの関数命令文においてそれらを括弧に入れる必要がある。
These variables are local to the function as with any variable in the function.
これらの変数は、関数のどんな変数と同様に、 関数へのローカルである。
To pass values in just put the values in the brackets when you are calling the function.
あなたが関数を呼んでいるとき、値を渡すことはちょうど値を括弧に入れた。

To pass a value out of the function you have to put the variable you want to pass out after the ENDFUNCTION command like above.
関数から値を取り出す為に、あなたはあなたが上記のような ENDFUNCTION命令の後で出て行くことを望む変数を置かなければならない。
Now you can either store this function in a variable or pass it straight into another command or function, like normal commands.
あなたが この関数を変数に保管することができるか、もう一つの命令または関数に それを直接 引き渡す事ができる今、通常の命令が好きになりなさい。

Functions are mainly useful for code re-use.
関数は、主にコード再利用に役立つ。
If you have a massive bit of code that takes up many lines and you use it 10 times, every time with different values, you don’t want to have to write it out 10 times, each time just a bit different, you want to put it in a function once and pass the different values in each time you need it.
もし、あなたが多くの線を巻きとるコードの大きいビットを持っている、そして、あなたが10回それを使うならば、毎回異なる値で、あなたは10回それを書かなければならなくしたくない、ちょうど少し異なるたびに、あなたがそれを必要とするたびに、あなたはかつてそれを関数に入れて、異なる値を渡したい。

Now, what if you wanted to have a variable that is accessible to all functions, like the number of times you wanted to print different things.
その時、あなたが全ての関数にアクセスできる変数を持ちたかったとしたら、時間の数の様に、あなたは異なるものをプリントしたかった。
You’d use the GLOBAL command to declare the variable as global:
広域(GLOBAL)であるように、あなたは変数を宣言しなさいという広域的なGLOBAL命令を使うだろう:

`Set number of times to print everything
GLOBAL numberOfTimes = 3
printHello()
printGoodbye()
printDarkBasic()
wait key
end
function printHello()
  for x=1 to numberOfTimes
     print “Hello”
  next x
endfunction
function printGoodbye()
  for x=1 to numberOfTimes
     print “Goodbye”
  next x
endfunction
function printDarkBasic()
  for x=1 to numberOfTimes
     print “DarkBasic”
  next x
endfunction

The GLOBAL command makes the variable available to all the code, including the functions.
GLOBAL命令は、関数を含む全てのコードが利用できる変数になる。

Arrays are by default global, that means that normally all arrays can be accessed by all functions.
配列はデフォルトで広域(GLOBAL)である、それは通常、全ての配列が全ての関数によってアクセスされることができることを意味する。
If you wanted to make an array local to a function you would declare it with the LOCAL command.
もし、あなたが関数にローカル配列を作りたいならば、あなたは LOCAL命令でそれを宣言するだろう。
Here is a very handy piece of code to demonstrate this…
ここに、示すコードの非常に便利な部分は、ここにある

function makeArrayThenDeleteIt()
  LOCAL dim veryUsefulArray(0)
  undim veryUsefulArray(0)
endfunction

One last simple thing.
1つの最後の単純なもの。
If you want to exit the function before the end of the function use EXITFUNCTION.
もし、あなたが関数の終わりの前に 関数から脱出したいならば、EXITFUNCTIONを使いなさい。
It works exactly the same as endfunction, just you are allowed to have as many as you want in the function (so long as you do have an ENDFUNCTION as well) and you can have them inside IF statements and such.
それは endfunctionと同じように正確に働く、ちょうどあなたはあなたが関数(あなたが同様に ENDFUNCTIONを持つ限り)で望む、そして、あなたが命令文その他ならばそれらを持つことができるだけを持ってもよい。

print divide(1.0,4.0)
print divide(3.0,0.0)
wait key
end
function divide(num1#,num2#)
  LOCAL result#
  if num2# = 0.0
     print “Cannot divide by 0”
     exitfunction 0
  else
     result# = num1# / num2#
  endif
endfunction result#

That code tests if the second parameter is zero (division by zero equals infinity), and if it is then the sum is not done, as it is impossible, a message is printed and the EXITFUNCTION command exits the function and returns 0.
そのコードは第2のパラメータがゼロ(ゼロによる割り算は、無限に等しい)であるかどうか調べる、そして、それがそうであるならば、合計はされない、それが不可能で、メッセージはプリントされる、そして、EXITFUNCTION命令は関数とRETURN0を出る。

Summary of Commands

命令の概要

· FUNCTION functionName([param1[,param2]…]) - Represents the start of a function and is where you define the function parameters and their types.
FUNCTION functionName()[param1[param2]…] - 関数の始まりを示して、あなたが関数パラメータとそれのタイプを定めるところである。

· ENDFUNCTION [returnValue] - Represents the end of the function and is where you can specify an optional return value.
ENDFUNCTION[returnValue] - 関数の終わりを意味して、あなたがオプションの戻り値を指定することができるところである。

· EXITFUNCTION [returnValue] - Like ENDFUNCTION except you can use as many as you want, wherever you want in your code, as long as they are inside the function.
EXITFUNCTION[returnValue]-あなたがあなたのコードで望むどこにでも、それが関数内部にいる限りあなたがあなたが望むのと同じくらい多くのものを使うことができること以外は、ENDFUNCTIONが好きになりなさい。

· GLOBAL var - Defines a variable/array as global.
グローバル変数 - 変数/配列を広域(GLOBAL)であると定義する。
This means that it can be accessed anywhere in your code.
これは、それがあなたのコードでどこでもアクセスされることができることを意味する。
Arrays are global by default.
配列は、デフォルトで広域である。

· LOCAL var - Defines a variable/array as local.
ローカル変数 - 変数/配列を局所(local)と定義する。
This means it can only be accessed inside the function it was declared.
これは、それがそうであった関数が宣言したことがアクセスされた内部でありえるだけのことを意味する。
Variables are local by default.
変数は、デフォルトでローカルである。

End of Section Tasks

セクション終わりの作業

l Make a set of functions that asks your name, age, telephone and other information, one function for each piece of information.
あなたの名前、年齢、電話と他の情報(情報の各々の部分のための1つの関数)を尋ねる一組の関数を行いなさい。
Make it so that you ask the name first and then you use the user’s name to ask questions in the other functions.
あなたが最初に名前を尋ねるように、たどり着きなさい、それから、あなたは他の関数で質問をするためにユーザーの名前を使用する。
The print out may be something like - “What is your name?
プリントアウトはすばらしいかもしれない-「あなたの名前は、何であるか?
Bill”, “How old are you Bill?
ビル」(「老人があなたである方法は、広告する?)
27”, “What is you’re telephone number Bill?” and so on.
27」「あなたであることは、電話番号議案である?」、そして、その他。