[Top][All Lists]

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

Re: Constants in functions?

From: Sergei Steshenko
Subject: Re: Constants in functions?
Date: Thu, 5 Mar 2020 20:36:08 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0

On 05/03/2020 20:12, Windhorn, Allen E [ACIM/LSA/MKT] wrote:
What's the accepted way to define a constant inside a function?  Example:
function [sc, err] = symmcomp(abc)
   % Function accepts [abc] complex components and delivers symmetrical
   % components as a vector [x0, x1, x2] of complex values
   persistent a = complex(-0.5, sqrt(3)/2);    % Rotation vector
   % Build conversion matrix_type (constant)
   persistent A = [1, 1, 1;
                1, a, a^2;
                1, a^2, a]./3;
   % Make abc a column vector
   if (length(abc)>3)
     sc = [];
     err = "Too many components!";
   elseif (length(abc)<3)
     sc = [];
     err = "Too few components!";
     abc = reshape(abc,[3,1]);
     sc = A*abc;
     err = "";

Here a is a constant and A is a constant matrix.  I used persistent in hopes
it would prevent them from being redefined every time the function is called.
In C++ I would use the "const" keyword.


An ugly (because IIRC Octave doesn't have nested functions) way:

function retval = TWO()

  retval = 2;



The above function will return 2. You can similarly return matrix, row o column vector, etc.


reply via email to

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