returning values through functions calls 
Author Message
 returning values through functions calls

I never really learned if APL can return a function value via a function
call like Pascal can for example.

I know how to call a function and pass a single value:
ie.
define a function:

      {del}SQRT X
[1]  X*.5
[2]  {del}

to call it I use:

      SQRT 10
3.16227766

now how can I pass the result of the function to another variable using a
statement ?

ie A{<-}SQRT 10

of course this doesn't work, but can I make APL do it using the function
name ?

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 ?

Mark



Fri, 14 Jan 2005 20:14:52 GMT  
 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



Fri, 14 Jan 2005 22:41:23 GMT  
 returning values through functions calls
thanks for that comprehensive explanation.

Although it looks clear in your UPPERCASE/lowercase examples, I was
wondering for a while how APL could determine the function name in the
definition, but obviously it establishes the function name depending on the
number of arguments used when defining it.

I will be modifying a couple of functions I made to use local variables,
because I was starting to accidently re-use the same variable names in
different functions, which can cause weird results, especially when each
function was tested independantly, worked, then had a few unexplainable
problems when combined. As a general comment, my variable names were short
and no-descriptive {I guess the trademark of APL programmers :) }, so adding
them as locals won't make the code much bigger. Besides I don't really need
all those temporary variables clogging up the workspace. They are handy for
debugging, but after that they aren't needed.

I have printed the APLX reference manual .pdf (in two thick halves) and find
it quite usefull when it comes to filling in some gaps, or getting specific
about primitives, however it fell a bit short as far as teaching basic
concepts like my question. Not that I'd expect it to include basics.

learning Finish is not my idea of an easy method of learning APL, English is
still a challenge for me most of the time !

cheers from the land down under,

Mark


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



Sat, 15 Jan 2005 21:10:38 GMT  
 returning values through functions calls
even though I can't read Finish, that link below doesn't seem to be there
and the index.html on that site was about ReCycling in Finland. I know APL
is old, so I guess you could say a re-cycled language :)

Mark

Quote:
> 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



Sat, 15 Jan 2005 21:18:17 GMT  
 returning values through functions calls
oops, it appears the APLX manual has some info on page 28/29, however it
falls short when it comes to good examples compared to those that you
provided Veli-Matti.

Mark

Quote:
> I have printed the APLX reference manual .pdf (in two thick halves) and
find
> it quite usefull when it comes to filling in some gaps, or getting
specific
> about primitives, however it fell a bit short as far as teaching basic
> concepts like my question. Not that I'd expect it to include basics.



Sat, 15 Jan 2005 21:52:55 GMT  
 returning values through functions calls



Quote:
> even though I can't read Finish, that link below doesn't seem to be there
> and the index.html on that site was about ReCycling in Finland. I know APL
> is old, so I guess you could say a re-cycled language :)

Oops - sorry for that.
Somehow the link doesn't work, but http://www.pyr.fi/apl/ does ;)

Yours
- Veli-Matti



Sat, 15 Jan 2005 21:54:47 GMT  
 returning values through functions calls
Get the book "APL2 at a Glance" by Brown, Pakin and Polivka.  It refers
to APL2 but there's a lot of good stuff in it and it's intended for an
introduction.  If you want to try APL2, download TryAPL2 for free from
IBM at
http://www-4.ibm.com/software/ad/apl/

Quote:

> problems when combined. As a general comment, my variable names were short
> and no-descriptive {I guess the trademark of APL programmers :)

Correction:  A beginning APL programmer.  I comment my own code.  I have
functions still in use that I wrote in the '60s.  If I want to update or
extend them, I want know what was going on.  BUT, and this is important,
don't put *what* the code does in the comment, put *why* it's doing it.
As you become fluent in APL, it will be obvious what is being done but
you may not remember why.  Also, some comments at the beginning of your
function to say what it does, what the argument(s) are and what is the
form of the result will be *very* welcome even a couple months down the
road.

Quit worrying so much about the size!  Even back in the days of 32K byte
workspaces, this wasn't that much of a problem.  I have over 850
functions, almost 500 variables and 6 defined operators in my personal
library and the whole thing takes up just over 1MB on my hard drive - I
could fit it on a floppy.

Feel free to ask questions or call for help.

Ted



Sun, 16 Jan 2005 00:25:29 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. return multiple values from an awk function?

2. returning values from a function in a shell script

3. multiple values in a return from function

4. function-return-values() in MacMarlais

5. multiple return values for functions

6. Function return values revisted (response to response)

7. functions with more than one return values and local v

8. question about writing to the return value of a system function using vpi_put_value()

9. How to return value in PLI function

10. Initialising a constant with a return value from a function

11. RETURN-Value of function

12. Getting a float return value from a c function

 

 
Powered by phpBB® Forum Software