func()

Read(3028) Label: func,

Here’s how to use func() function.

func fn(a=v,…) … {return x i }

Description:

Define a subroutine block.

Syntax:

func  fn(a=v,...)

{return xi}

Note:

The function defines a subroutine block, which may or may not return a result after computation.

 

The subroutine name is fn. The subroutine begins with the starting characters func fn(a=v,...) and covers a cell range where the master cell is the one holding the func fn(a=v,...). The return statement is used to return the function’s result and terminate the execution of the function block. The subroutine will by default return the value of the last calculation cell if no return statement appears during execution.

Parameter:

fn

Subroutine name.

a

Defined parameters.

An undefined parameter headed by $ represents a string; it does not needed to be quoted when being called. The parameter string will be directly passed to the subroutine without being computed or processed. A parameter defined in the format of “$parameter name” is  a macro parameter.

v

Default value of parameter a.

Option:

@i

Use this option to increase performance when the subroutine does not contain a recursive operation.

@m

 

Call a parameter in the form of macro. The function body consists of only one expression where the other variables should not be changed.

@o

Use this option to define a subroutine. In the invocation expression fn@o, o is the first (string) parameter value. If no option is specified in the invocation expression, the first (string) parameter value passed is regarded as null.

Return value:

Result of the function block

Example:

 

A

B

 

1

func ft(arg1=3)

 

Define a subroutine named ft, where arg1 is the parameter whose default value is 3, and return B2’s value after the computation is finished.

2

 

=arg1*10

3

 

return B2

 

4

=ft(2)

 

Call subroutine ft, pass 2 to it as the parameter value and return 20.

5

=ft()

 

Call subroutine ft where the parameter uses default value and return 30.

 

When @o option is present:

 

A

B

 

1

func ft@o(opt,s)

 

Define a subroutine named ft using @o method; both opt and s are parameters used by the subroutine;

We can use @u option, which transforms string s to the uppercase and @1 option, which transforms string s to the lowercase, with subroutine ft; the function returns the length of string s when no option is present.

2

 

=if(opt=="u":upper(s),opt=="l":lower(s);len(s))

3

 

return B2

4

=ft@l("Hello")

 

Call subroutine ft during which @1 option transforms string "Hello" to the lowercase, and return "hello".

5

=ft@u("Hello")

 

Call subroutine ft during which @u option transforms string "Hello" to the uppercase, and return "HELLO".

6

=ft("Hello")

 

Call subroutine ft, where no option is used, to compute the length of string "Hello" and return 5.

 

When a macro parameter is used:

 

A

B

 

1

func f1($arg1)

 

Define a subroutine named f1, where arg1 is a macro parameter and does not need to be quoted when being called, and compute and return the length of the string parameter arg1.

2

 

=len(arg1)

3

 

return B2

4

=f1("abc")

 

Call subroutine f1, pass string "abc" to f1 as as whole, and return  5.

5

=f1(abc)

 

Call subroutine f1 and return 3.

 

When @m option is present:

 

A

B

 

1

func MA@m(fd1,n )

 

 

Define a subroutine using the @m option and call it in the macro form.

2

 

=fd1[-n:-1].avg()

3

 

 

4

=file("D:/srd.txt").import@t()

 

Return a table sequence:

5

=A4.derive(MA(Closing,5):MA5 )

 

Call the subroutine in the form of macro to compute the 5-day moving average and add it to table sequence A4 as a new column:

A5’s call of the subroutine for computation is equivalent to executing expression  =A4.derive(Closing[-5:-1].avg():MA5).