October 30, 2020, 10:53:13 pm

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Global or local variable

Started by aurelCB, August 27, 2020, 02:20:10 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

August 27, 2020, 02:20:10 pm Last Edit: August 27, 2020, 02:22:50 pm by aurelCB
hello
..to continue from topic about syntax coloring in rich edit
Is here anyone who can explain to me how same variable name can be
used as global and then defined as local to subroutine ;
like this :

'This function adjusts the colors of the keywords on the given linenum
SUB colorize(w1 , ctl, linenum)
def lineStart, lineLength:int
def i, ctl, linenum, char_start: int
def p, pLine:int

variable is linenum and is integer -> int
forum for hobby programming:<br />https://aurelsoft.ucoz.com

billhsln

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
When all else fails, get a bigger hammer.

Andy

Bill is 100% correct.

To prove it, see attached program and screen shot.

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

aurelCB

I 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 !
forum for hobby programming:<br />https://aurelsoft.ucoz.com

billhsln

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
When all else fails, get a bigger hammer.

Brian

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

aurelCB

I don't know if this ia a error in code or have purpose
is a mistery to me ..
also i don't know who is author of this code or from where I have it ????
forum for hobby programming:<br />https://aurelsoft.ucoz.com

aurelCB

..and yes when i remove linenum from local set of declartion i get 
error that variable is not defined ...ok as expected 
but how both of them work is really crazy thing  :o

...and another fact 
I replaced in CB code 
lineStart = CONTROLCMD (w1, 1 ,char_start, linenum)
'which also not make a sense ....with 0 message,,,yes NULL
like this:

lineStart = Sendmessage (w1, 0 ,char_start, linenum, 1)

then work !!!....do you beleive??
forum for hobby programming:<br />https://aurelsoft.ucoz.com

jalih

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.


aurelCB

yes but with same name ..which is not very smart
forum for hobby programming:<br />https://aurelsoft.ucoz.com

jalih

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:

Code Select

 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...