Clear Filters
Clear Filters

How do you name single output functions?

10 views (last 30 days)
The matlab style book reccomends to name single output functions like their output, which is consistent with the MALTAB functions...
I dont like this very much, because I want the variable of the output of my function be called like the output...
How do you name your single output or single output function?
I would probably use something like
function result = calculateResult(input)
% calculateResult calculates Result ;D
altough I dont like it that much

Accepted Answer

Jan on 6 Feb 2021
I use CamelCase for my funtions to reduce the danger a collision with toolbox functions of Matlab. For frequently used functions, compact names are nice. A leading "Calculate..." would introduce too much redundancy, because most functions calculate something. Compare:
CalculateMean(CalculateSin(CalculateLinSpace(0, 2*CalculatePI, 1000)))
mean(sin(linspace(0, 2*pi, 1000)))
How the output of a function is called, does not matter, when the function is called. Then the code of the function is not visible and the naming does not influence the usability.
Sometimes, I use the name of the procedure, sometimes the name of the output, and sometimes I add an initial "Set", "Get", "Write", "Read" etc. An example for different approachs:
MD5 = GetMD5(File)
MD5 = MD5Hash(File)
Hash = MD5(File)
Some fixed styles for choosing names of functions and variables are useful. But as long as you keep the overview and hte names do not get too long or too cryptic, it remains a question of taste. Then spending time and energy in the naming conventions is a wasted optimization.
On the other hand I'm using conventions for all names of variables:
  • sulkingCamelCase for internally used variables, CamelCase for onput and output.
  • All names in singular, trailing "List", "Vec" etc. if it matters, that they are arrays.
  • Leading "is..." or "do..." for logical flags.
  • "n..." is a number of elements or objects, "i..." is a loop counter.
This reduces the chance of e.g. confusing "nFile" and "nFiles" anywhere in the code. In opposite to this, I see less problems with confused names of functions.
  1 Comment
Maximilian Schönau
Maximilian Schönau on 6 Feb 2021
Thank you!
I guess I won’t use calculate anymore, and think of a different Name for the same thing within the function it is calculated...

Sign in to comment.

More Answers (2)

madhan ravi
madhan ravi on 5 Feb 2021
result = @(in) in.^2;
result = result(1 : 10)
result = 1×10
1 4 9 16 25 36 49 64 81 100
Walter Roberson
Walter Roberson on 6 Feb 2021
Functions having the same name as their result is only possible with functions handles like you did.
Not true. For example,
sum = sum([1:5; randi(10,1,5)])
sum = 1×5
3 8 13 13 6
sum is not a function handle, it is a full function, but you have successfully used it both as a function name and as the name of a variable.
Also that is not practical cause the function gets overriden
What would your ideal interface be, that would permit you to distinguish between calling a function named result, vs using or indexing a variable named result?
Who would your ideal readers of your code be, that they would never be confused between whether result was referring to a function or a variable?
x = result %is that calling result or is it copying the variable named result?
Maximilian Schönau
Maximilian Schönau on 6 Feb 2021
Hi, I probably didn’t made it clear, but I was referring to the variable name within the function...
And a function declaration of
function mean = mean(y)
Would not be possible.
As to your last Statement, I would always write:
x = result();
I also do this when calling methods with no inputs on my class.

Sign in to comment.

Steven Lord
Steven Lord on 6 Feb 2021
function result = calculateResult(input)
% calculateResult calculates Result ;D
I have some feedback on this.
First, the identifier input already has a meaning in MATLAB so I wouldn't use it as a variable name.
Second, I dislike that function name because it's so bland. It offers no hint of what the function does. "calculate" and "Result" in this context are both plain filler words. In my opinion reading the name of the function should give me a reasonable idea of what the function actually does and ideally I should not need to read its help text or its documentation unless I need details about some specific nuance of the function. [Yes, I know that not all functions in MathWorks products satisfy this. I did use the word "ideally" in my description. Plus we've learned a bunch about designing our software over the past couple decades.]
Think of it like Burger King, Kentucky Fried Chicken, or Pizza Hut. Even if you've never heard of one of these businesses before, likely you could guess just from the names what they are (restaurants) and what they sell (burgers, fried chicken, and pizza respectively.) As a MATLAB example, consider the islocalmax function. Without reading that function's help text or documentation, what would you guess it does?
Third, it shouldn't matter what you name the output variable inside your function. Ideally the user of your function should not care how the sausage is made, just that the sausage they ordered is the sausage they received. [The health inspector should care, but in this metaphor the health inspector is played by the white-box tests of your function. And even the health inspector probably shouldn't care what nicknames you have for the pieces of equipment in your kitchen unless you call the meat grinder "The Salmonella Factory."] There's nothing tying the name of the variable in your function that you assign as the first output to the name of the variable to which that output is assigned in your user's code. There's effectively no difference between (using your calculateResult function)
y = calculateResult(1:10)
abracadabra = calculateResult(1:10)
qwerty{1} = calculateResult(1:10)
% Assuming the caller called sin on one of the above
z = sin(calculateResult(1:10)) % the output of calculateResult is stored in an unnamed temporary
Maximilian Schönau
Maximilian Schönau on 6 Feb 2021
I hope I did not sound, as if I find the separation of a function name and it’s output not necessary, it obviously is.
I am just a bit annoyed, that using the function mean as an example, I have to use a variable within the function mean, that will be the output and therefore the mean, but I am not allowed to call it that way.
Again, I don’t know how you could avoid this problem in a smart way, that was the reason I asked this question here :)
I found out, that calling my function mean „calculateMean“ so that I am free to use mean as an variable in that function probably is in most cases not the best solution.
Walter Roberson
Walter Roberson on 7 Feb 2021
I have seen programming languages in which the method to return a value was to assign to a variable with the same name as the function. However, the name could not otherwise be used as a variable inside the program, and could not be indexed. So for example it would have been valid to do the equivalent of
double mean(double x) {mean = sum(x)./numel(x);}
However, such languages have challenges. For example,
double mean(double x) {mean = 0; mean = mean + mean(1) + mean(x./2);}
Is the mean(1) indexing? Is it function evaluation?
double mean(double x) { memzero(&mean, sizeof(mean)); }
is memzero being passed the address of the output variable for the function, or is it being passed a pointer to the function ?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!