help-octave
[Top][All Lists]
Advanced

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

loading the functions automatically


From: John W. Eaton
Subject: loading the functions automatically
Date: Tue, 19 Jun 2007 02:43:14 -0400

On 18-Jun-2007, Kamaraju S Kusumanchi wrote:

| Using Debian Etch, octave 2.9.9
| 
| Consider the following script
| 
| $cat even_odd.m
| 1;
| function even_odd1(m)
|   # use bitand instead of mod to figure out if a number is odd or even
|   # if (mod(m, 2) == 1)
|   if (bitand(m,1))
|     printf("%d is odd\n", m)
|   else
|     printf("%d is even\n", m)
|   endif
| endfunction
| 
| n = 25
| even_odd1(n)
| n = 30
| even_odd1(n)
| 
| 
| I am able to run the script as
| 
| $octave -q
| octave:1> even_odd
| n =  25
| 25 is odd
| n =  30
| 30 is even
| octave:2> 
| 
| After that, I can do
| 
| octave:2> even_odd1(23)
| 23 is odd
| octave:3> 
| 
| That is the function even_odd1 is loaded into the memory. Now if I go back
| to even_odd.m and change the function even_odd1, then the changes are not
| reflected back on the octave command line.

Don't define the function in a script file.  Instead, just define the
function in a function file:

  $ cat even_odd.m
  function even_odd(m)
    # use bitand instead of mod to figure out if a number is odd or even
    # if (mod(m, 2) == 1)
    if (bitand(m,1))
      printf("%d is odd\n", m)
    else
      printf("%d is even\n", m)
    endif
  endfunction

Then tehre is no need to run the script to define the function.
Octave will just load the function when you call it:

  octave:1> even_odd (23)
  23 is odd

If you modify the function, it should be reloaded.

Functions defined in scripts are not automatically reloaded because
doing that would have potentially odd side effects.

jwe



reply via email to

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