# Constants in functions? Classic List Threaded 5 messages Open this post in threaded view
|

## Constants in functions?

 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!";   else     abc = reshape(abc,[3,1]);     sc = A*abc;     err = "";   endif endfunction ________________________________________ 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. Regards, Allen -- Allen Windhorn P.E. (Mn), CEng| Senior Principal Engineer Leroy-Somer Americas | Kato Engineering, Inc. 2075 Howard Dr. West | North Mankato, MN 56003 | USA T +1 507-345-2782 | F +1 507-345-2798 [hidden email] | [hidden email]
Open this post in threaded view
|

## Re: Constants in functions?

 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!"; >    else >      abc = reshape(abc,[3,1]); >      sc = A*abc; >      err = ""; >    endif > endfunction > ________________________________________ > > 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. > > Regards, > Allen An ugly (because IIRC Octave doesn't have nested functions) way: function retval = TWO()    retval = 2; endfunction . The above function will return 2. You can similarly return matrix, row o column vector, etc. --Sergei.
Open this post in threaded view
|

## Re: Constants in functions?

 On 3/6/20 3:36 AM, Sergei Steshenko via Help list for GNU Octave wrote: > > 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!"; >>    else >>      abc = reshape(abc,[3,1]); >>      sc = A*abc; >>      err = ""; >>    endif >> endfunction >> ________________________________________ >> >> 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. >> >> Regards, >> Allen > > > An ugly (because IIRC Octave doesn't have nested functions) way: > > function retval = TWO() > >   retval = 2; > > endfunction > > . > > The above function will return 2. You can similarly return matrix, row o > column vector, etc. > > > --Sergei. > Dear Allen, Your code seems fine to me.  Using "persistent"  avoids the variables to be redefined each time the function is invoked.  And if you do not change it's value, it is "constant".  So what it is exactly you are worried about? Do you fear the value to be changed accidentally (by whom)?  How should this happen, when you write the function?  In that case you can create a subfunction , as suggested by Sergei, plus defining "retval" as "persistent" for performance reasons (the variable "retval" would be recreated in each function call).  I do not see a benefit of nested-functions  in this scenario. Neither Octave nor Matlab have a concept of C++ "const" variables.  Only within classdef classes you can define constant properties .  But the overhead may not be justified. HTH, Kai  https://octave.org/doc/v5.2.0/Persistent-Variables.html https://octave.org/doc/v5.2.0/Subfunctions.html https://octave.org/doc/v5.2.0/Nested-Functions.html https://octave.org/doc/v5.2.0/Properties.html