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

조회 수: 4 (최근 30일)
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
Jan
Jan 2022년 2월 18일
Please post a relevant part of the code, which reproduces the problem. "But they don't" is not precise enough to understand, what happens.

댓글을 달려면 로그인하십시오.

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2022년 2월 18일
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
Jan
Jan 2022년 2월 18일
@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 2022년 2월 18일
@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"...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by