How to get global variables recognized in main and in functions at the bottom

1 view (last 30 days)
I have a homework file that says "place global variables here" at the top and then "algorithm here" below that in a for loop and then I can put any helper functions at the bottom.
So I want my helper functions to see those static variables at the top.
But they don't. I have to copy and paste into the function bodies. This is so redundant and silly.
What is a work around? I am not sure I am allowed to put static variables in another file like I did with my functions I am reusing from a previous homework I made a class file and class encapsulating those functions as class methods.
  1 Comment
Jan
Jan on 18 Feb 2022
Please post a relevant part of the code, which reproduces the problem. "But they don't" is not precise enough to understand, what happens.

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 18 Feb 2022
Do not do this task as given. Learn to avoid global variables like the plague. Write a minimal function and test-script that illustrates that you understand how to define and handle global variables. Something like this:
% Test script, illustrating global variables:
global GlobVar
GlobVar = 12;
GlobVar_prev = GlobVar;
x = sqrt(2);
x2 = add_globvar2x(x);
disp([x,x2])
disp(GlobVar)
disp(GlobVar_prev)
and a test-function:
function x2 = addglobvar2x(x)
global GlobVar
x2 = x + GlobVar;
Globvar = 123*randn(size(GlobVar)+1);
end
Then you solve the task you've been given but use additional input-arguments instead of the global variables. For the handling of the variables inside the function and the subfunctions or nested functions search for the documentation on nested functions.
HTH
  5 Comments
Jan
Jan on 18 Feb 2022
@Nobutaka Kim: If you are aware, that global variables impede the debugging and should be avoided in productive code, everything is fine. If your task is to write a short hack, which will never grow beyond a limit of 1000 or 2000 lines of code, there is no problem.
Professional programmer try to avoid repeated work. So if a piece of code is useful, it is stored in a way, which allows to re-use it in arbitrary other code. Usually this means a function with the minimum risk with interferences with other functions. Then global variables are evil.
There are some (almost) professional function in the FileExchange, whose programmers decided to use globals for the ommunication between the set of subfunctions. At the beginning, this did not look like a problem, but now the code have grown to huge toolboxes and it is very hard to refacture them from scratch using a clean providing of inputs and outputs.
Software engineering is a demanding science. I've seen too many projects for teaching, which simply negelects this important topic. It is like using tools without keeping them clean. But, of course, we will not solve this problem here in the forum.
Bjorn Gustavsson
Bjorn Gustavsson on 18 Feb 2022
@Nobutaka Kim: But then you should be fine solving the task in an efficien, clean, neat and maintainable way. If push come to shove then you can always argue that the solution you've made is exactly that "efficient, clean neat and maintainable and gets the job done"...

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!