HyperMath

Defining Functions

Defining Functions

Previous topic Next topic No expanding text in this topic  

Defining Functions

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

Functions are conventionally defined in HyperMath with the function keyword as follows:

    function function_name (args)

    body

    end

The following example shows a simple function called "foo" that receives a single argument and returns twice its value (in the command window, it needs to defined on a single line as shown below):

      > function foo(n)

        return n*2

        end

      > print(foo(7))

      14

There is also a format similar to C-style:

  function function_name (args)

  {

  body

  }

Multiple input arguments are separated by commas, for example the function foo(n,m).  A variable number of arguments can also be passed.  See Variable Number  of Arguments for more information.  There is no need to specify the number of returns, however.