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:
Global variables vs std. variables passing

Subject: Global variables vs std. variables passing

From: Mateusz Gos

Date: 16 Mar, 2009 11:40:18

Message: 1 of 3

I was just wondering (for quite a while actually) whether there are any drawbacks of using global variables as an alternative to passing variables to function handles using an anonymous function syntax (alternatively nested function). Is any of these two solutions preferred?

Thank you in advance,
Mateusz

Subject: Global variables vs std. variables passing

From: Bruno Luong

Date: 16 Mar, 2009 12:00:19

Message: 2 of 3

"Mateusz Gos" <webmaster24@wp.pl> wrote in message <gpldr2$p7t$1@fred.mathworks.com>...
> I was just wondering (for quite a while actually) whether there are any drawbacks of using global variables as an alternative to passing variables to function handles using an anonymous function syntax (alternatively nested function). Is any of these two solutions preferred?

Function handles all the way. A global variable modified in various places (especially triggered by Callbacks) is a best way to get a headache when debugging. It is also vulnerable to command such as

CLEAR ALL % It can happen anywhere

etc...

Bruno

Subject: Global variables vs std. variables passing

From: Bruno Luong

Date: 16 Mar, 2009 12:23:13

Message: 3 of 3

Another argument against GLOBAL is "reusable". A piece of code and algorithm is well built when it's reusable (the contrary is not necessary true). The exact same piece of code/function can be used in various applications. This will eliminate using systematically GLOBAL.

Bruno

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