Sintaxis de nivel superior
Prev | Chapter 7. Programación avanzada con GEL | Next

## Sintaxis de nivel superior

The syntax is slightly different if you enter statements on the top level versus when they are inside parentheses or inside functions. On the top level, enter acts the same as if you press return on the command line. Therefore think of programs as just a sequence of lines as if they were entered on the command line. In particular, you do not need to enter the separator at the end of the line (unless it is part of several statements inside parentheses). When a statement does not end with a separator on the top level, the result is printed after being executed.

For example,

    function f(x)=x^2
    f(3)

will print first the result of setting a function (a representation of the function, in this case `(`(x)=(x^2))`) and then the expected 9. To avoid this, enter a separator after the function definition.

    function f(x)=x^2;
    f(3)

If you need to put a separator into your function then you have to surround with parenthesis. For example:

    function f(x)=(
      y=1;
      for j=1 to x do
        y = y+j;
      y^2
    );

The following code will produce an error when entered on the top level of a program, while it will work just fine in a function.

    if Something() then
      DoSomething()
    else
      DoSomethingElse()

The problem is that after Genius Mathematics Tool sees the end of line after the second line, it will decide that we have whole statement and it will execute it. After the execution is done, Genius Mathematics Tool will go on to the next line, it will see `else`, and it will produce a parsing error. To fix this, use parentheses. Genius Mathematics Tool will not be satisfied until it has found that all parentheses are closed.

    if Something() then (
      DoSomething()
    ) else (
      DoSomethingElse()
    )

Prev | Up | Next
Chapter 7. Programación avanzada con GEL | Home | Devolver funciones