PRI-(BASIC2) Table of content Introduction...2 New Comment...2 Long variable...2 Function definition...3 Function declaration...3 Function return value...3 Keyword return inside functions...4 Function parameters...4 Function call...5 Passing arguments by reference...5 Recursive function call...6 Local labels...7 Local variables...7 Misc... 8 Goto... 8 MEMORY( prog var tot )...8 TYPE$( varname )...8 PRI-(BASIC2)_v2 Page 1/8
Introduction The aim of this document is to provide additional information about the new BASIC 2. Basically, everything working in the Script Editor (previous tool to code some BASIC) will work in the BASIC 2. Key features of BASIC 2: long name variable new comment syntax function definition function call new BASIC instructions New Comment You can still use the old REM keyword but we added a more standard one: // REM this is a comment // this is also a comment Long variable You can now define long name for variables typed as INTEGER or STRING Compared to the Script Editor, where only a one character variable (A% or A$) could be used, it's a big improvement in terms of readability and code refactoring. Variable names are case insensitive (myint% and MyInt% are the same variable). Long names are also applicable on ARRAY ex: DIM arrayofstring(25,80) A% = 2 // old way to define an Integer, only 26 possible variables my_variable% = 1 // new way to define an Integer, unlimited number of variables DIM A$(25,80) // old way, only 26 array of strings DIM arrayofstring(25,80) // new way, unlimited number of array of strings DIM A(25,80) // old way, only 26 array of floats DIM arrayoffloat(25,80) // new way, unlimited number of array of floats PRI-(BASIC2)_v2 Page 2/8
Function definition Function declaration To declare a function, you now have two keywords: Function Function is used to start the function definitions and is followed on the same line by the function name. is used to end the function definitions. Function my_function print my_string // function definition begin // function definition end Function return value You specify the function return value using the following function name convention: If your function returns an integer: Function my_function% If your function returns a string: Function my_function$ If your function returns a float: Function my_function To specify the return value of a function, an implicit variable is created automatically based on your function name. When your function exits, the return value is the last value of this variable. The Following function will print my_string on your console but the return value will be 2. Because we initialized $my_function to 1 and add 1 to this variable. When the function exits (after the print), the last value of $my_function is 2. Function my_function $my_function = 1 $my_function = $my_function + 1 print my_string PRI-(BASIC2)_v2 Page 3/8
Keyword return inside functions You can use the keyword return at any place inside a function to end it. The current value of the return value ($FunctionName) will be returned as if we reached the. Function my_function if (global_var%=1) then $my_function = 1.0 return endif $my_function = 0.0 Function parameters You can define and apply parameters to your function. These parameters are typed (same way as functions). Here is a definition of a function with three parameters: Function my_function($param1, $param2%, $param3$) $my_function = $param2% + $param1 + 1 print @my_function(3, 3, 3 ) Properties of this parameters: Parameters are put between parenthesis and separated by a coma. Parameters are, by default, passed by value. Parameters type is deduced by the naming convention '$' at the end for string '%' at the end for integer nothing at the end for float Parameters are local variables in the function scope. These function parameters don't exist outside the function. To clarify this distinction with standard variables: every parameter variable begins with '$' in the declaration and inside the function. This allows you to manipulate global and local variable with the same name without messing up. PRI-(BASIC2)_v2 Page 4/8
Function call To call a function, you just need to write '@' followed by the function name and between parenthesis the parameters values. If there is no parameters, you can also omit the parenthesis. Function my_function($param1) print call of [my_function] with param [ ;$param1; ] Function my_function2() print my_function2() Function my_function3 print my_function3() @my_function(3) @my_function2 @my_function3() // call of a function without parameters // call of a function without parameters Pay attention to float and int parameters, if you give a float as an integer parameter (or the opposite), an implicit cast will occur. Function my_function($param1%) print call of [my_function] with param [ ;$param1; ] @my_function(3) // OK @my_function(3.4) // KO Passing arguments by reference By default the parameters are passed by value. This means that you cannot execute side effects. But sometimes, you want side effects to exist. A simple example would be a function where you want to return 3 values. PRI-(BASIC2)_v2 Page 5/8
If you precede the parameters with '@', they will be passed by reference. You can then use them as a normal parameter inside your function. The only difference compared to a normal parameter (passed by value) is that all changes made inside the function will be visible outside this function. In this case, types of parameters are Function my_function(@$param1,@$param2,@$param3$) $param1 = $param1 * 2 $param2 = $param2 * 2 $param3$ = "my_function_string" v1 = 1.5 v2% = 2 v3$ = "my_string" @my_function(v1, v2%, v3$) print v1 print v2% print v3$ Recursive function call You can call a function inside a function: function exp($x, $n%) if ($n% = 1) then $exp = $x else if ($n% mod 2 = 0) then $exp = @exp($x * $x, $n% / 2) else $exp = $x * @exp($x * $x, ($n% - 1) / 2) endif endif endfn exec: print @exp(3, 3) PRI-(BASIC2)_v2 Page 6/8
Local labels Sometimes, it's useful to have labels only inside your function to ease the flow control, but without polluting the name spaces of your program. To solve this, you are able to define local labels in your functions. To declare a local label, the syntax is the following: begin with '$', followed by your label name and end it by ':'. You can now use 'goto' inside your function to move to this label ('goto $YOUR_LABEL_NAME'). Outside the function, this label doesn't exist. Function test_label(): $a% = 1 goto $exit $a% = 2 $exit: $test_label = $a% endfn print @test_label() Local variables Local variables are used to define variables visible only in the function scope. Just declare a standard variable but precede it with the '$' character inside your function. function a() $b = 3 // local variable b $a = $b + 3 endfn exec: print @a() // here, @a() exists, but not $b. PRI-(BASIC2)_v2 Page 7/8
Misc Goto The goto function will move your execution cursor to a defined label. You can even pass string in a goto. my_label: print my_string end goto my_label A$ = my_label goto A$ MEMORY( prog var tot ) Returns the free memory of the Basic. prog will return the free memory of the program zone var will return the free memory of the variable zone tot will return prog + var TYPE$( varname ) Return the type of a variable. Will be string, float, or integer PRI-(BASIC2)_v2 Page 8/8