what does () mean in the line below?
x=somefunction()

 채택된 답변

Wayne King
Wayne King 2011년 11월 7일

0 개 추천

Then somefunction.m does not take input arguments for example:
function x = somefunction
x = 2;
end
if you call
x = somefunction()
you get x = 2
you can also just enter
x = somefunction;

추가 답변 (2개)

Wayne King
Wayne King 2011년 11월 7일

0 개 추천

The way you've written it, it implies that you are calling a function called somefunction that returns an output x. Inside the parentheses is where you supply any input arguments, name-value pairs, etc.

댓글 수: 1

Baba
Baba 2011년 11월 7일
but in the code that I have, there are no parameters inside the parenthese.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 11월 7일

0 개 추천

Then it is a function call with no parameters supplied.
Ordinary MATLAB functions that do not need a parameter, can be called either with the () notation or just by giving the function name. For example,
x = pi + rand;
is equivalent to
x = pi() + rand();
as "pi" and "rand" are both functions in MATLAB.
However, if somefunction were a function handle, then
x = somefunction;
would not call the function: instead it would copy the function handle. But
x = somefunction();
would invoke the function whether it was a regular function or a function handle.

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

태그

질문:

2011년 11월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by