Trouble Using Functions

classic Classic list List threaded Threaded
7 messages Options
DrD
Reply | Threaded
Open this post in threaded view
|

Trouble Using Functions

DrD
I've written a short script to use functions, and it does not run for me (it says the first function encountered is not defined). Any help would be appreciated. Here is my script:

% TestFile1
p=[1;2;3]
q=[3;4;7]
ssq=s2(q);
[add,subt]=s1(p,q);

function [sum,diff]=s1(a,b)
  print "   Function s1 -- sum and difference"
  sum=a+b
  diff=a-b
endfunction
function sumsq=s2(a)
  print "   Function s2 -- sum of squares"
  sumsq=a'*a
endfunction

Virus-free. www.avast.com


Reply | Threaded
Open this post in threaded view
|

Re: Trouble Using Functions

Doug Stewart-4


On Wed, Jun 24, 2020 at 3:28 PM Samuel Doughty <[hidden email]> wrote:
I've written a short script to use functions, and it does not run for me (it says the first function encountered is not defined). Any help would be appreciated. Here is my script:

% TestFile1
p=[1;2;3]
q=[3;4;7]

function [sum,diff]=s1(a,b)
  print "   Function s1 -- sum and difference"
  sum=a+b
  diff=a-b
endfunction
function sumsq=s2(a)
  print "   Function s2 -- sum of squares"
  sumsq=a'*a
endfunction




ssq=s2(q);
[add,subt]=s1(p,q);


try again with these lines moved to the end. 
Virus-free. www.avast.com



--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: Trouble Using Functions

apjanke-floss
In reply to this post by DrD


On 6/24/20 3:28 PM, Samuel Doughty wrote:

> I've written a short script to use functions, and it does not run for me
> (it says the first function encountered is not defined). Any help would
> be appreciated. Here is my script:
>
> % TestFile1
> p=[1;2;3]
> q=[3;4;7]
> ssq=s2(q);
> [add,subt]=s1(p,q);
>
> function [sum,diff]=s1(a,b)
>   print "   Function s1 -- sum and difference"
>   sum=a+b
>   diff=a-b
> endfunction
> function sumsq=s2(a)
>   print "   Function s2 -- sum of squares"
>   sumsq=a'*a
> endfunction
>

Hi Sam,

That's in Matlab format, where local functions go at the end of a
script. In Octave, functions inside a script need to come before they
are used.

Cheers,
Andrew


DrD
Reply | Threaded
Open this post in threaded view
|

Re: Trouble Using Functions

DrD


On Wed, Jun 24, 2020 at 2:44 PM Andrew Janke <[hidden email]> wrote:


On 6/24/20 3:28 PM, Samuel Doughty wrote:
> I've written a short script to use functions, and it does not run for me
> (it says the first function encountered is not defined). Any help would
> be appreciated. Here is my script:
>
> % TestFile1
> p=[1;2;3]
> q=[3;4;7]
> ssq=s2(q);
> [add,subt]=s1(p,q);
>
> function [sum,diff]=s1(a,b)
>   print "   Function s1 -- sum and difference"
>   sum=a+b
>   diff=a-b
> endfunction
> function sumsq=s2(a)
>   print "   Function s2 -- sum of squares"
>   sumsq=a'*a
> endfunction
>

Hi Sam,

That's in Matlab format, where local functions go at the end of a
script. In Octave, functions inside a script need to come before they
are used.

Cheers,
Andrew

Thank you Doug and Andrew.

When I move the function definitions to the beginning (right after the header line that reads %TestFile1.m), I still get errors, specifically --

warning: function name 's1' does not agree with function filename 'C:\Users\Sam\Desktop\Octave\Tests\TestFile1.m'
error: print: no figure to print

Evidently there is something more that I'm missing.
Sam

Virus-free. www.avast.com


Reply | Threaded
Open this post in threaded view
|

Re: Trouble Using Functions

nrjank
Administrator
> warning: function name 's1' does not agree with function filename 'C:\Users\Sam\Desktop\Octave\Tests\TestFile1.m'
> error: print: no figure to print
>

the warning is just telling you that normally the main function in a file should match the filename.  it's just a warning, so you can ignore it.

the error:  I don't think Octave's print function works as a command like you have it written for displaying text.  see


it's really just meant for printing/saving plot images.

you might want to use disp() or printf()


Reply | Threaded
Open this post in threaded view
|

Re: Trouble Using Functions

José Abílio Matos
In reply to this post by DrD
On Wednesday, 24 June 2020 21.03.15 WEST Samuel Doughty wrote:

> Thank you Doug and Andrew.
>
> When I move the function definitions to the beginning (right after the
> header line that reads %TestFile1.m), I still get errors, specifically --
>
> warning: function name 's1' does not agree with function filename
> 'C:\Users\Sam\Desktop\Octave\Tests\TestFile1.m' error: print: no figure to
> print
>
> Evidently there is something more that I'm missing.
> Sam

See the second answer here:
https://stackoverflow.com/questions/54013267/run-octave-script-file-containing-a-function-definition

that is due to the basic distinction between functions and scripts.

The line

1;

ensures that it is not a function and thus it is a script.

--
José Matos




DrD
Reply | Threaded
Open this post in threaded view
|

Re: Trouble Using Functions

DrD


On Wed, Jun 24, 2020 at 3:18 PM José Abílio Matos <[hidden email]> wrote:
On Wednesday, 24 June 2020 21.03.15 WEST Samuel Doughty wrote:
> Thank you Doug and Andrew.
>
> When I move the function definitions to the beginning (right after the
> header line that reads %TestFile1.m), I still get errors, specifically --
>
> warning: function name 's1' does not agree with function filename
> 'C:\Users\Sam\Desktop\Octave\Tests\TestFile1.m' error: print: no figure to
> print
>
> Evidently there is something more that I'm missing.
> Sam

See the second answer here:
https://stackoverflow.com/questions/54013267/run-octave-script-file-containing-a-function-definition

that is due to the basic distinction between functions and scripts.

The line

1;

ensures that it is not a function and thus it is a script.

--
José Matos


Thanks, guys. It works now.

Sam



Virus-free. www.avast.com