Clear Filters
Clear Filters

Function Call without parentheses

40 views (last 30 days)
Robert
Robert on 7 Feb 2024
Commented: Stephen23 on 7 Feb 2024
I was given some code where I have a SPEARClass class, but when I debug the code, I am confused as to how a function is being called.
Inside SPEARCLASS is a function SetThreatInfo, defined by
function obj = SetThreatInfo(obj)
Outside of the SpearClass, in a different file, I write these two lines
  1. SPEAR=SPEARClass;
  2. SPEAR=SPEAR.SetThreatInfo;
and I somehow end up inside the SetThreatInfo function despite not attaching a variable or using parentheses at all. I am confused to how this occurs, can you call a function without parentheses in matlab?
  1 Comment
Stephen23
Stephen23 on 7 Feb 2024
"I am confused to how this occurs, can you call a function without parentheses in matlab?"
Yes.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 7 Feb 2024
In MATLAB, naming a function with no parameters is equivalent to calling the function with no parameters.
This is not the case for function handles
foo = zeros() %calls zeros with no parameters
foo = 0
bar = zeros %calls zeros with no parameters
bar = 0
zerofcn = @zeros; %creates function handle
baz = zerofcn %copies function handle
baz = function_handle with value:
@zeros
biff = baz() %invokes function stored in baz with no parameters
biff = 0

Steven Lord
Steven Lord on 7 Feb 2024
Calling a function without parentheses and with 0 input arguments
Yes, you can call a function without parentheses. In fact, you have almost certainly done it before without realizing it.
which -all pi % pi is a built-in function
built-in (/MATLAB/toolbox/matlab/elmat/pi)
y = 2*pi % Calls the pi function without parentheses and with 0 input arguments
y = 6.2832
y = 2*pi() % Calls the pi function with parentheses and with 0 input arguments
y = 6.2832
Calling a function without parentheses and with input arguments
There's another way to call functions without parentheses but with input arguments. That is command syntax as opposed to function syntax.
help pi % Equivalent to help('pi')
PI 3.1415926535897.... PI = 4*atan(1) = imag(log(-1)) = 3.1415926535897.... Documentation for pi doc pi
help('pi')
PI 3.1415926535897.... PI = 4*atan(1) = imag(log(-1)) = 3.1415926535897.... Documentation for pi doc pi
Calling class methods without parentheses
But in this case, you're talking about calling a class method which is a little different. You used dot syntax as opposed to function syntax. In the case where your non-Static method is called with only one input argument, the two syntaxes are equivalent. You're in this case, so "SPEAR=SPEAR.SetThreatInfo;" is equivalent to "SPEAR = SetThreatInfo(SPEAR);".
In the case where your method is called with two or more inputs in certain cases they can behave differently if multiple objects with certain precedence relationships are involved, but that's a little bit of an advanced maneuver (and is described in the Object Precedence section on the page I linked at the start of this paragraph.)
I think most of the times when you'd need to care about object precedence is if you're overloading operators for your class or you want to allow calling plot on your object with an axes handle as an optional input and want to make sure your object's plot method is the one that's called. In that case you probably wouldn't want to use dot syntax to call plot.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!