Contents Previous Next Subchapters

Defining Functions With Arguments
Syntax function name(arguments) block
See Also defining functions , argument number

Defines a function with one or more arguments.

The arguments list contains variables that are used to pass information to and from a function.

     function square(x) begin
          return x * x
defines a function that returns the square of its argument. To evaluate the function at x = 3, enter
     print square(3)
and O-Matrix will respond

A function can have any number of arguments. If you enter
     function hypotenuse(a, b) begin
          return sqrt(a^2 + b^2)
     hypotenuse(3., 4.)
O-Matrix will respond

If a variable name is passed as an argument (not a constant or an expression) the value of the variable can be changed inside of the function. For example, entering
     x = 5
     function f(y) begin
          y = 6
     print x
results in
because the value of x is changed inside of the function. On the other hand, if you continue the example above by entering
     const z = 4
     print z
O-Matrix will respond
because z is a constant. In addition, if you continue this example by entering
     w = 3
     print w
O-Matrix will respond
because w(3) is an expression and not simply a variable name.

Note that in all cases above, the value of y, after the assignment of the function f, is 5.