Got Questions? Get Answers.
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:
Use Functions with input/output or global variables

Subject: Use Functions with input/output or global variables

From: Peter

Date: 9 Nov, 2011 10:09:13

Message: 1 of 2

Dear all,
I have a couple of functions that are called from the main script, performing all different kind of analysis. Due to the fact that I collect the major parameters in a global variable I'm not sure what type of programming would be the best (both are working).
1. I can call the functions with input / output parameters, which might make them of more general use
2. I can just call the function, define my global variable and have all necessary variables directly by the hand. This solution might be more handy, because I also put some of the larger 'raw data' in the global variable as well and do not have to double the memory space for the function call...
Is there a general rule or advice?
I'm looking forward to your comments.
Best,
Peter

Subject: Use Functions with input/output or global variables

From: Loren Shure

Date: 9 Nov, 2011 16:19:47

Message: 2 of 2


"Peter " <peter.baeuerle@gmx.de> wrote in message
news:j9djg9$pdc$1@newscl01ah.mathworks.com...
> Dear all,
> I have a couple of functions that are called from the main script,
> performing all different kind of analysis. Due to the fact that I collect
> the major parameters in a global variable I'm not sure what type of
> programming would be the best (both are working). 1. I can call the
> functions with input / output parameters, which might make them of more
> general use
> 2. I can just call the function, define my global variable and have all
> necessary variables directly by the hand. This solution might be more
> handy, because I also put some of the larger 'raw data' in the global
> variable as well and do not have to double the memory space for the
> function call...
> Is there a general rule or advice?
> I'm looking forward to your comments.
> Best,
> Peter

My advice- make general functions that take relevant inputs and outputs.
Avoid global variables at almost all cost. If your functions are using the
inputs but not changing them, MATLAB wll not make physical copies of the
inputs during the function call.

--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/

Tags for this Thread

No tags are associated with 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