ユーザー関数の作成
素数を判定するルーチンを、ユーザー関数として、独立させて作ってみます。
'素数かどうかを判定する関数 Function isPrime(n) Dim I If Not IsNumeric(n) Then isPrime = False Exit Function End If If n < 2 Then isPrime = False Exit Function End If isPrime = True For I = 2 To Round(Sqr(n), 0) If (n Mod I) = 0 Then isPrime = False Exit For End If Next I End Function |
戻り値は関数名のisPrimeに代入します。
ここで感心したのが、Exit Forです。
BASICでは、FORループからは、途中で抜けられないと思っていたのですが、抜けられるようになったのですね。