Global variables and subroutines
Lets do the same thing as in the last example, except we will define
global variables, that is a variable defined outside a subroutine.
Unlike in the previous example, if change the value of the global
variable in Procedure_1 will change it everywhere and hence impact
it's use in Procedure_2.
<html>
<head>
<Title> Global variable</title>
</head>
<body>
<% strGlobal="I'm a persistent script-level variable"%>
<% strGlobalChange="I'm a changing global variable"%>
<%=strGlobal%><br>
<%=strGlobalChange%><br>
<% sub Procedure_1()
strDifferent="Hi I'am strDifferent in Procedure_1"
StrGlobalChange="Changed in procedure_1"
%>
<%=strDifferent%><br>
<%=strGlobal%><br>
<%=strGlobalChange%>
<%End sub%>
<% sub Procedure_2()
strDifferent="Hi I'am strDifferent in Procedure_2"%>
<%=strdifferent%><br>
<%=strGlobal%><br>
<%=strGlobalChange%>
<%End sub%>
<P> Calling Procedure 1 ..<I><%Procedure_1()%> </i></P>
<P> Calling Procedure 2 ..<I><%Procedure_2()%> </i></P>
<P> Calling Procedure 1 ..<I><%Procedure_1()%> </i></P>
</body>
</html>
How it works:
Each subroutine, Procedure 1 and procedure 2 define their own copies
of strDiffernt and assign different values to thereby demonstrating
how to create a local variable and the scope of that variable. They
also print out the variable strGlobal which is defined outside the
subroutines and hence can be seen by each subroutine. Hence the value
will be the same in both. Global variable gets changed in one subroutine
will impact its value every where.
|