The basics
- Functions are reusable blocks of code. To call a function means to do what's specified in the function.
function printText()
{
println( "text" );
}
printText();
- Basic function definitions have the following syntax:
function <name> ( <argument-list> ) { <code> }
- Argument list is a comma-separated list of names or nothing.
- Names, also called "identifiers", can have the folowing symbols: a-z, A-Z, _, 0-9, but they cannot start with a digit, because only numbers can start with a digit.
- User-defined names cannot collide with so-called keywords - special names, like
function
y = 15.51;
x = 3 * y + 10;
x * y;
- Each expression statement ends with ";".
- Useful expression statements include an assignment operator or a function call, or both.
- All expression statements presented so far - except the last one - are useful.
- The assignment operator is "=". It simply assigns the value on the right side to the item on the left side.
- Numbers use the point (".") as decimal digit separator.
- There are arithmetic operators available that take two items at each side (called "binary operators").
- The operators used in the example are those of addition ("+") and multiplication ("*").
include "math";
function sinc( x )
{
xpi = x * M_PI;
return sin( xpi ) / xpi;
}
sinc3 = sinc( 3 );
- Functions may return with or without data. When they do return data, that data can be retrieved from the call.
- A basic function call has the syntax
<name> <subexpression>
- A subexpression has the syntax
( <expression-list> )
- Expression list is a comma-separated list of expressions or nothing.
- The
include
statement loads a library or a code file.
math
is one of the few built-in libraries. In this example, it defines the function sin
and the constant M_PI
x = 1; y = 2;
x += y;
++x;
--y;
- Shortcuts exist for common operations: combined assignment operators and increment/decrement operators.
- Most non-assignment binary operators have their assignment counterparts. The difference is that they assign the calculated value to the left side of the expression.
- Increment/decrement operators are shortcuts for
x += 1
and x -= 1
, respectively.
- There are alternative versions of these operators that will be covered later.
x = 5; y = 3;
x *= y * ( y + x );
y -= x + x * y;
y += 5 * y += y;
printvar( y );
println( dumpvar( x ) );
- There's very few limits on how the expressions can be combined so it's up to you to write them in a way the meaning is clear both to you and the compiler.
- A very useful tool for finding out the contents of a variable are the
printvar
/ dumpvar
functions.
println
is a function that prints the given variables to standard output and moves write cursor to the next line.
- Order of arithmetic operations is mathematically correct: sub-expressions
( .. )
are evaluated first, then multiplication *
/ division /
/ modulo (remainder) %
, and then - addition +
and subtraction -
.
x = 5;
global y = 6;
function z()
{
a = 7;
global b = 8;
function c(){}
}
- Variables are the currency of this language.
- There are 4 types of variable storage, two of which are covered in the example: local and global variables.
- In the example, variables x, a, c are local and y, z, b are global.
- All new variables are local by default.
- The keyword
global
specifies a list of variables that are global and allows to assign their values.
- A function definition creates a global function variable outside other functions, local otherwise.
// testing the assignment operator
a = 5;
/* this ----------
-- should print --
------------- 5 */
println( a ); ////
- Code can contain comments. They can be used to communicate between various users and maybe even compilers of the code.
- The SGScript compiler completely ignores comments.
- There are two types of comments: single-line comments
// ...
and multiline comments / ... /