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

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 Commento

Please post a relevant part of the code, which reproduces the problem. "But they don't" is not precise enough to understand, what happens.

Accedi per commentare.

 Risposta accettata

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 Commenti

I agree that global variables are drilling a hole in your knee.
But the OP explains, that the homework question asks for "place global variables here". Maybe the intention is to let the students learn to see bad programming habits.
Yeah, that might be the objective (but I never ever needed to learn to see bad programming habits, they came ever so naturally to me...). For that I'd modify my example to use two functions with the same global variable declared:
% 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)
name = 'Odysseus';
Name_list = append_to_names(name);
name = 'Helen';
Name_list = append_to_names(name);
x3 = add_globvar2x(x2);
With:
function name_list = append_to_names(name)
global GlobVar
if ~ischar(GlobVar)
GlobVar = name; % Initialize global name-list
name_list = name;
else
name_list = char(GlobVar,name);
end
And if the OP wants to have variables visible in sub-functions without sending them in the function-call one might consider nested functions (though those are too close to global-variable territory for my preferences - but I don't have the "energy" to try to stop others from using that capability...).
The point of the exercise is not to teach any principle on coding habits because it is actually a robotics class and we are tasked with writing scripts to control robots in a simulator.
For the time being I am going to be redundant and just put the variables with assigned values that I need in each function.
@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.
@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"...

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2021b

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by