For functions that take arguments, the argument list should be provided as a comma-separated list in parentheses after the function name. For instance:

newAtom("C", 1.1, 0, 4.2);

Arguments may be constant values (as in this example), variables, arithmetic expressions, or any other function that returns the correct type. For functions that (optionally) do not take arguments, the empty parentheses may be omitted. A list of all functions, their arguments, and their return types is given in the Command Reference.

All functions return a value, even if it is 'no data' (i.e. void in C/C++). For instance, in the example above the newAtom command actually returns a reference to the Atom it has just created, and this may be stored for later use:

Atom a;
a = newAtom("C", 1.0, 1.0, 1.0);

However, if the return value of any function is not required then it may simply be forgotten about, as in the example prior to the one above.

User Defined Functions

Custom functions may be defined and used in Aten, taking a list of variable arguments with (optional) default values. The syntax for defining a function is as follows:

**type** **name** ( arguments ) { commands }

is one of the standard variable types and indicates the expected return value type for the function. If no return value is required (i.e. it is a subroutine rather than a function) then should be replaced by the keyword:

**void** **name** ( arguments ) { commands }

Once defined, functions are called in the same way as are built-in functions. The name of the function obeys the same conventions that apply to variable names, e.g. must begin with a letter, cannot be the name of an existing keyword, function, or variable. The arguments are specified in a similar manner to variable declarations. A comma-separated declaration list consisting of pairs of variable types and names should be provided, e.g.:

**void** **testRoutine** ( **int** _i_, **int** _j_, **double** _factor_ ) { ... }

Our new subroutine testRoutine is defined to take three arguments; two integers, i and j, and a double factor. All three must be supplied when calling the function, e.g.

printf("Calling testRoutine...\n");
int num = 2;
double d = 10.0;
testRoutine(num, 4, d);
printf("Done.\n");

When defining the function/subroutine arguments, default values may be indicated, and permit the function to be called in the absence of one or more arguments. For instance, lets say that for our testRoutine, the final argument facto is likely to be 10.0 on most occasions. We may then define a default value for this argument, such that if the function is called without it, this default value will be assumed:

**void** **testRoutine** ( **int** _i_, **int** _j_, **double** _factor_ = 10.0 ) { ... }

printf("Calling testRoutine...\n");
int num = 2;
testRoutine(num, 4);
testRoutine(num, 4, 20.0);
printf("Done.\n");

Both methods of calling testRoutine in this example are valid.

Return Values

For functions defined to return a specific type, at some point in the body of the function a suitable value should be returned. This is achieved with the return keyword. Consider this simple example which checks the sign of a numeric argument, returning 1 for a positive number and -1 for a negative number:

**int** **checksign** ( **double** _num_ )
{
      if (num < 0) return -1;
      else return 1;
}

If an attempt is made to return a value whose type does not match the type of the function, an error will be raised. Note that, once a return statement is reached, the function is exited immediately. For functions that do not return values (i.e. those declared as void) then return simply exits from the function – no return value need, or should, be supplied.

Arithmetic Expressions and Operators

Arithmetic operates in the same way as in C, and utilises the same operator precedence etc. Similarly, comparison operators (less than, equal to etc.) are the same as those found in C.