Contents Previous Next Subchapters

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

Description
Defines a function with one or more arguments.

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

Tutorial
Entering
     clear
     function square(x) begin
          return x * x
     end
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
     9


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

Reference
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
     clear
     x = 5
     function f(y) begin
          y = 6
     end
     f(x)
     print x
results in
     6
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
     f(z)
     print z
O-Matrix will respond
     4
because z is a constant. In addition, if you continue this example by entering
     w = 3
     f(w(1))
     print w
O-Matrix will respond
     3
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.