HP Forums
Should'nt global variables be available to subroutines? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Should'nt global variables be available to subroutines? (/thread-3387.html)



Should'nt global variables be available to subroutines? - Gene222 - 03-15-2015 07:16 PM

I got my HP Prime two week ago, so I am kinda new at this. I started working on a program with a subroutine, but the subroutine would not work unless all variables used in the subroutine were pass to the subroutine when I called the subroutine. Should not the global variables (in this case Q, N, D, and S) be accessible for use by the subroutine? Am I doing something wrong with my code? I though global variables and system variables (A through Z) were supposed to be accessible to all programs.

My other question is do I need to declare system variables A through Z in any program that uses those variables? I find the concept of system variables, local variables and export command somewhat confusing.

Thanks, Gene

My program thus far:[/code][/php]

Sub1();

// Main program
local X1, X2, FX1, FX2;
EXPORT Pipe(Q,N,D,S)
BEGIN
input({Q,N,D,S},
"Pipe Flow",
{"Q cfs = ",
"n value = ",
"Dia ft = ",
"Slope ft/ft = "});
// Initial X values where X = y/D
X1:=0.5;
X2:=0.8;

// Determine f(x1)
FX1:=Sub1(X1,Q,N,D,S);
print (FX1)

END;

// Subroutine to calc f(x)
Sub1(X,Q,N,D,S) //Why should Q,N,D,S need to be included?
BEGIN
local theta, A, R;
theta:= acos(1 - 2*X);
A:= (D^2 / 4) * (theta - 0.5*sin(2*theta));
R:= (D/4) * (1 − sin(2 * theta) / (2*theta));
return (1.486/N)*A*R^(2/3)*S^(1/2) - Q;
END;


RE: Should'nt global variables be available to subroutines? - Gene222 - 03-15-2015 09:41 PM

Found my answer. For good articles on programming the HP Prime, see the Article Forum on the main page of this website. They are much more detail than the HP manual. Take a look at:
HP Prime Programming: An Introduction by Han
HP Prime Programming: Variable types and their properties by Han