Anyone Know What the Syntax `Pi()` Means in Matlab?
We Accidentally Used Pi() in Matlab Instead of Pi, However It Works Just as Pi (The Built-in Constant). >> Pi() Ans = 3.1416 >> Pi Ans = 3.1416 but We Cannot...
We accidentally used pi() in Matlab instead of pi, however it works just as pi (the built-in constant).
>> pi()
ans =
3.1416
>> pi
ans =
3.1416
But we cannot understand the syntax pi() here in the Matlab language. We tried to interpret it as a function but it returned an error that it cannot take input arguments.
>> pi(1)
Error using pi
Too many input arguments.
Can anyone enlighten us about what we are missing out on here? Thanks.
2 Answers
pi is actually function that takes in no inputs. That's why if you call pi() and pi, it results in the same thing because they both are calling pi with no inputs. In general, doing f and f() where f is a function, they both mean the same thing where f is being called with no inputs.
Consult the official documentation for more details: . Under the heading Input Arguments (optional) it reads:
If your function accepts any inputs, enclose their names in parentheses after the function name. Separate inputs with commas.
function y = myFunction(one,two,three)
If there are no inputs, you can omit the parentheses.
... so that's why pi and pi() mean the same thing.
Edit: pi() is a function in matlab. It returns 3.1415.... However, if a function doesn't have arguments, matlab doesn't need the '()'. That's why you can just type pi. It's quite clever of them really. Matlab coding syntax feels optimized for mathematical clarity.
Another example of this is the filesep() command. You can just type filesep and it will work. If I could just make this a comment, I would.