Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Programming language should be strict!

Subject: Programming language should be strict!

From: Yevgeny Gayev

Date: 8 Jun, 2013 10:38:08

Message: 1 of 4

Dear all,
here is Prof. Gayev (Ukraine). I use MATLAB fruitfully for teaching programming for students.
Indeed, it is a wonderful media that may illustrate all the conceptual programming topics required, and, contrary to other languages, to come to the end very quickly. It is especially convenient for beginners.

I met a problem for the MATLAB v. 2012a that never met before.
It is natural that programming block, say, FOR ends with its END. Similar with other structural blocs, WHILE . . . END, IF . . . END, etc. The key word of any m-file FUNCTION is not a block and so it shouldn’t end with any END. However, if one closes one’s m-file with such an extra END, the program does work! Why is this?
I know that this is required for, say, Pascal. However, any language should strictly follow to its own rules. Why is MATLAB (of elder versions I guess) so ‘soft’? Why does it allow this extra END?

Subject: Programming language should be strict!

From: Nasser M. Abbasi

Date: 8 Jun, 2013 10:43:51

Message: 2 of 4

On 6/8/2013 5:38 AM, Yevgeny Gayev wrote:

> I met a problem for the MATLAB v. 2012a that never met before.

>The key word of any m-file FUNCTION is not a block and so it shouldn’t
>end with any END. However, if one closes one’s m-file with such an extra END,
>the program does work! Why is this?

If you add an END to the function to close it, then all other
functions in the same m file should also have an END.

You can either use an END or not to close a function. Both will work.
But you can't mix and match in the same m file.

I do not know when an END was added for a function closing, but as
far as I remember, this was always an optional. Someone else might
know the history better. I prefer to add an end myself, but
GUIDE for example, generates m files without an END.

--Nasser

Subject: Programming language should be strict!

From: Yevgeny Gayev

Date: 8 Jun, 2013 11:10:09

Message: 3 of 4

Dear Nasser, I'm very thankful to you!

Ye.Gayev

"Nasser M. Abbasi" wrote in message <kov1t3$ln1$1@speranza.aioe.org>...
> On 6/8/2013 5:38 AM, Yevgeny Gayev wrote:
>
> > I met a problem for the MATLAB v. 2012a that never met before.
>
> >The key word of any m-file FUNCTION is not a block and so it shouldn’t
> >end with any END. However, if one closes one’s m-file with such an extra END,
> >the program does work! Why is this?
>
> If you add an END to the function to close it, then all other
> functions in the same m file should also have an END.
>
> You can either use an END or not to close a function. Both will work.
> But you can't mix and match in the same m file.
>
> I do not know when an END was added for a function closing, but as
> far as I remember, this was always an optional. Someone else might
> know the history better. I prefer to add an end myself, but
> GUIDE for example, generates m files without an END.
>
> --Nasser

Subject: Programming language should be strict!

From: Doug Schwarz

Date: 8 Jun, 2013 12:18:50

Message: 4 of 4

In article <kov1ig$8i7$1@newscl01ah.mathworks.com>,
 "Yevgeny Gayev" <ye_gayev@voliacable.com> wrote:

> Dear all,
> here is Prof. Gayev (Ukraine). I use MATLAB fruitfully for teaching
> programming for students.
> Indeed, it is a wonderful media that may illustrate all the conceptual
> programming topics required, and, contrary to other languages, to come to the
> end very quickly. It is especially convenient for beginners.
>
> I met a problem for the MATLAB v. 2012a that never met before.
> It is natural that programming block, say, FOR ends with its END. Similar
> with other structural blocs, WHILE . . . END, IF . . . END, etc. The key
> word of any m-file FUNCTION is not a block and so it shouldnt end with any
> END. However, if one closes ones m-file with such an extra END, the
> program does work! Why is this?
> I know that this is required for, say, Pascal. However, any language should
> strictly follow to its own rules. Why is MATLAB (of elder versions I guess)
> so soft? Why does it allow this extra END?

The reason is nested functions which can be placed anywhere in your
code. Something must indicate the end of a nested function. Once you
have used an END statement to end one function you must do so for all
functions in that m-file. That rule may be a little too strict -- for
example, logically, the final END statement is not needed. I think of
not using the END as the special case as it works only when your m-file
consists of a main function and subfunctions and no nested functions.
It is the most common programming structure for beginners, however.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us