help-octave
[Top][All Lists]
Advanced

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

Re: Are Octave script files compiled or interpreted?


From: Mike Miller
Subject: Re: Are Octave script files compiled or interpreted?
Date: Fri, 4 Oct 2013 15:15:01 -0400

On Fri, Oct 4, 2013 at 11:50:16 -0700, Torpedo93 wrote:
> Hello, so I was wondering are script files compiled or interpreted? I read
> the some tutorial manuals online and they said that Octave will only look
> for definitions inside the functions until they are needed. Therefore, does
> it mean that it will only look for the definitions when the function is
> evaluated?

Yes, scripts are interpreted and symbols are looked up when they are
referenced, whether the symbol refers to a variable in Octave's
workspace or a function in the path. If a function is called inside of
a conditional branch that happens to be false, for example, that
function doesn't have to be defined.

> I think I have tested a basic script file:
> ## Name of the file is second.m
> clear;
> function h = square(x)
>         h = addi(x)^2;
> endfunction
>
> function g = addi(x)
>         g = x+5;
> endfunction
>
> ========================
> After I start my code by typing in "second" (w/o the "") in the command
> window, it seems like it doesn't matter whether I had put the function
> addi(x) before or after the function square(x).

Where is the call to the function square? Is that in your script or do
you enter it separately at the command prompt? In this case, assuming
you aren't calling square until after addi is defined, then you are
correct that it doesn't matter.

> I was also wondering is it a good practice to not use a function name that
> is the same name as the file name? Because I had received errors messages
> from the command window, however my code still ran properly.

It's important to understand the difference between a script and a
function file. Both are written in the same Octave language. A script
just executes the commands listed in it, including any function
definitions, like you have above.

A function file starts with a function definition and it must have the
same name as the file. Any subfunctions defined after the main
function are private, only visible to the functions in that same file.

HTH,

-- 
mike


reply via email to

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