IonicWind Software

Creative Basic => GUI Programs => Topic started by: aurelCB on August 27, 2020, 02:20:10 PM

Title: Global or local variable
Post by: aurelCB on August 27, 2020, 02:20:10 PM
hello
Title: Re: Global or local variable
Post by: billhsln on August 27, 2020, 03:06:01 PM
Variables defined within a function are local to that function.  You can use the variable that is defined in the main program any where, but when it is defined in a subroutine, it is local and will not effect the global version, whereas, if it is not defined in the function then it changes the global.

Bill
Title: Re: Global or local variable
Post by: Andy on August 27, 2020, 09:48:08 PM
Bill is 100% correct.

To prove it, see attached program and screen shot.

Andy.
Title: Re: Global or local variable
Post by: billhsln on August 28, 2020, 10:06:38 AM
A normal compiler would have marked that as a problem.  So, the question becomes, if you change the variable in the subroutine does it stay local or does it update the global one?

Technically the compiler should have given you an error on this.

Bill
Title: Re: Global or local variable
Post by: Brian on August 28, 2020, 11:10:53 AM
As far as I am aware, a definition defined inside a SUB is for that SUB only, and is destroyed on leaving the SUB

Brian
Title: Re: Global or local variable
Post by: jalih on August 29, 2020, 03:57:19 AM
Quote from: aurelCB on August 28, 2020, 03:23:03 AMI agree and i know that very well
but both of you miss my point
look into red variables again:

SUB colorize(w1 , ctl, linenum)
def lineStart, lineLength : int
def i, ctl, linenum, char_start ; int
def p, pLine :int


do you see now ,one time is same variable defined as argument of function and then
is again defined as local...so i am wondering again what is a point in this
if we load some value into argument var-> linenum
then again is terminated by local declaration linenum : int
i am sorry but not make any sense to me !

I see no double declarations. Function argument types are declared at the function body.

Title: Re: Global or local variable
Post by: jalih on August 29, 2020, 12:45:33 PM
Quote from: aurelCB on August 29, 2020, 11:35:54 AMyes but with same name ..which is not very smart

They are the same! As an old PL/I programmer, I have used to declaring types for procedure arguments at the body:


 test: proc options(main);

  call hello(5);

  hello: proc(n);
    dcl n fixed bin(31);

    dcl i fixed bin(31);

    do i = 1 to n;
      put skip list('Hello World!');
    end;

  end hello;

 end test;

So, hello procedure takes n as parameter but compiler can't know the type of it until it's declared as signed 32 bit integer...