Global variables. How to write global variables?

조회 수: 6 (최근 30일)
dovis838
dovis838 2018년 12월 14일
답변: Adam Danz 2018년 12월 14일
I need to create scripts and functions in which data is exchanged only through global variables. I need to create a script with vector from 2*pi to -2*pi with step -0.1; and this script should calculate this function:
and I have created script and 2 functions.
Script:
The answer is good but I need to make global variables and I have no clue how to overwrite this script ant functions. Any ideas?
  댓글 수: 2
James Tursa
James Tursa 2018년 12월 14일
편집: James Tursa 2018년 12월 14일
Why do you think you need to use global variables? It is usually best to avoid them and simply pass around your variables via the input/output argument lists, like you already have with the PIRMAS(x) and ANTRAS(x) functions. Rather than rewriting your functions to use global variables, I would write your calling code to use argument passing (which it looks like it already does).
dovis838
dovis838 2018년 12월 14일
Idk, my professor gave this task to try global variables and realize how it works... -,- and I know that it is useless but still, I need to manage somehow to do this task

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

답변 (1개)

Adam Danz
Adam Danz 2018년 12월 14일
Sounds like a wild assignment. Anyway, here's an example.
global x %declare global var
x = 5; %give global var a value
rn = drawRand(7);
function y = drawRand(n)
global x %within a separate function, declare the same global var
y = rand(n,1) .* x;
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by