help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Function calls (Was: Save a plot to .ps)


From: John W. Eaton
Subject: Function calls (Was: Save a plot to .ps)
Date: Fri, 28 May 1999 09:30:41 -0500 (CDT)

On 28-May-1999, Andy Adler <address@hidden> wrote:

| However, 
|   The current situation is ambiguous
|  
|   function foo (a)
|     eval (a);
|     bar (a);
| 
|   function bar (option)
|     fprintf ('bar=%f\n', option);
| 
| If we call 
|   >> foo('bar=[3,4];a=2');
| 
| Then is bar a matrix or a function call?
| 
| In this situation:
| 
| 1) OCTAVE      octave:16> foo('br=[3,4];a=2;')
|                ans = 4 
| 
| 2) MATLAB      >> foo('br=[3,4];a=2;')
|                bar=2.000000
| 
| ( So it seems that matlab is precompiling the functions and ignores
|   the output of eval )

I think it's a bug to ignore what could happen in the eval(), since
`eval (STRING)' is supposed to be equivalent to evaluating `STRING' in
the current context.  If you write

  function foo (a)
    bar = [4, 3];
    a = 2;
    bar (a);

I believe that Matlab will see that bar is a variable and will index
it instead of calling the function bar.

| Anyway, it seems that out of the ambiguity is simply to define a
| precedence order. Something like
| 
| 1. If bar is a variable   bar -option is a substraction
| 
| 2. If bar is a function   bar -option => bar('-option')
| 
| 3. If bar is a function with no inputs that returns a value,
|     then force the user to code this as bar() -option
| 
| Is this a reasonable solution? 

I don't think so.  When Octave parses a function, it has now way of 
knowing that a symbol is a function or a variable, until the code is
actually evaluated.  That's why you can write

  function foo ()
    ...
    bar ();
    ...
  end

  function bar ()
    ...
    foo ();
    ...
  end

in the same script file and have it work.  That's also why eval() can
do the right thing.

Do you really want Octave to require you to only define functions in
.m files?  Or to require that if you define them on the command line
or in a script file, that you must define all functions before they
are called?  That rules out any mutually-recursive functions defined
on the command line or in script files and forces you to define other
sets of functions from the bottom up.

FWIW, here is the way I think this should have been designed:

  * Function calls always have to use `()', even when there are no
    arguments.

  * Indexing should have used a different operator (perhaps `[]')
    instead of `()', so that there would never be any confusion about
    whether an expression is a function call or an indexed variable.

  * The operator for building matrices should have been something
    different too (perhaps `{}').

but of course, that is not the way Matlab works, so if you want
compatibility, you are stuck with some annoyingly ambiguous syntax.

| I think that the convenience of being able to call
| function without having to press the shift-("') keys (which are
| a pain for a vi user like myself) would be really nice.

Sorry, but I disagree, so it is very unlikely that I will even try to
implement a `feature' like this.  If someone else does it, great, but
you will have to work pretty hard to convince me that it is something
that should go into any version of Octave that I distribute.

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]