returning values through functions calls

Quote:

> I never really learned if APL can return a function value via a function

> call like Pascal can for example.

....

> also what makes a variable a local variable, or is it up to the programmer

> to determine through standard conventions.

> for example in the above example can I use X in the function, but have a

> separate independant X outside the function call ?

All you need is a little lesson about the APL function header:

[res <-] [l_arg] FUN [r_arg] {;locals}

first, your function may have zero, one or two arguments

(they are correspondingly called as niladic, monadic and dyadic functions in

APL literature),

and your function may or may not give a result, thus you can make six

different types of functions:

z <- FUN

z <- FUN rarg

z <- larg FUN rarg

FUN

FUN rarg

larg FUN rarg

It is usually advisable to use functions which give a result, e.g.

[0] z <- y PLUS x

[1] z <- y + x

[0] z <- SQUARE x

[1] z <- x * 2

now you can chain the functions as

100 PLUS 20 PLUS SQUARE 3 PLUS 4

which gives something like 169 ;)

(Of course you can assign the function results to a variable etc.)

If you assign a value inside a function, it will become/replace a global

variable, unless you declare it local by putting it in the header line.

Complicated? - not, but clumsy with huge functions and long variable names.

BTW, the result, right and left arguments are always local by default.

Let's have a variable

VAR <- 3.14

and a function

[0] z <- x PYTH y

[1] VAR <- (x * 2) + y * 2

[2] z <- VAR * 0.5

and you execute:

3 PYTH 4

5

The unwanted side effect: the VAR variable has changed to 25!

To avoid this, you can put the variable name in the header separated with

semicolon:

[0] z <- x PYTH y ;VAR; SQ

[1] VAR <- (x * 2) + y * 2

[2] SQ <- VAR * 0.5

[3] z <- SQ

And then execute lines

var <- 3.14

3 PYTH 4

5

VAR will still contain its original value, and there's no SQ in the

workspace.

To get more precise information you can

1) try to find a good APL primer (e.g. Gilman & Rose)

2) learn Finnish and then load a Finnish APL Guide from the FinnAPL site

www.pyr.fi/APL

3) try to find an English guide, e.g.

http://www.microapl.co.uk/APL/APLXLangRef.pdf

may explain the needed/missing details

Happy APLing

- Veli-Matti