difference between command and function

조회 수: 23 (최근 30일)
gang gyoo jin
gang gyoo jin 2016년 12월 8일
답변: Korada Saikumar 2021년 2월 25일
What is the difference between command and function in MATLAB ? cos(x), fzero, sprintf... are introduced as commands in some books. Is it right?
  댓글 수: 2
gang gyoo jin
gang gyoo jin 2016년 12월 8일
Many thanks. But I am still confused. Are Plot and subplot commands or functions?
Stephen23
Stephen23 2016년 12월 8일
편집: Stephen23 2016년 12월 8일
@gang gyoo jin: they are all functions. And they are also all commands. All functions can be used as commands, so there is no difference between functions and commands in terms of their functionality. The difference is in the syntax of how they are called, not in the functionality of what they do. The different syntax is useful in different situations (command syntax is much more limited in what it can do).
As my answer showed, any function may be called using function syntax, and also command syntax.
To make your life easier just call them all "functions", and just ignore the different name "command".

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

답변 (2개)

Stephen23
Stephen23 2016년 12월 8일
편집: Stephen23 2017년 1월 14일
There is no difference between functions and commands, the terms can be used to refer to the same functionality, the only difference is in how they are written, as the official MATLAB documentation explains:
The different syntax is shown here:
>> str2double('32') % function syntax (with parentheses)
ans = 32
>> str2double 32 % command syntax (without parentheses)
ans = 32
Note that with command syntax all trailing letter characters are treated as strings, and spaces separate input arguments. This often confuses beginners. In practice command syntax is useful only in some limited situations when using the command window. Do no use it for writing real code in scripts or functions.

Korada Saikumar
Korada Saikumar 2021년 2월 25일
A=2 B=3 A+B

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by