2017년 5월 3일 수요일

How does variables influenz another?


I have a problem with a variable.

I defined a global item as a list.

then I called a function with the parameter of this variable
the function changed a value of the parameter and
returned a value, but while working with the lst parameter the original global itemvariable is also changed.

In the exemple you see how I copied it to different parameters.p.ex. list = global item

my question: the variable list, is it only another name for the same variable, so using it I use  global item?
And:  Is it only for variables containing a list or for other too?

I seems so. I copied the variable with the copy list (list) item. And then the error left (I hope)
Testing with ordinary variables showed, that may not be so.

In the tutorials I didn't found it. Maybe I jumped over it. But if it is so, this behaviour may cause Problems.




--
the function changed a value of the parameter and returned a value, but while working with the lst parameter the original global item variable is also changed.
this is called call-by-reference, see also here https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference

to copy lists, use the copy list block

--
For the shortest possible proof of this, see my Ouroborous example:

(also in the Gallery)

--
Thanks for the link. I think I will not try this app- :)

--
Thanks, that was new for me. but it was hard to understand for me in all details. (language and subject) But I understood the principles.

Only one question left: I tried to manipulate in the same way text containing variables. (for to test)
But that does not work. So are only list variables called by reference? So could Numbers and text be send to another variable  like var b = var a or do I have to aspect also interference und bugs doing so?

--
Thanks, that was new for me. but it was hard to understand for me in all details. (language and subject) But I understood the principles. 

in Wikipedia this is also explained in further languages



So are only list variables called by reference?

yes


So could Numbers and text be send to another variable  like var b = var a 

yes

--
Back in the early 1970's, I had much fun with the FORTRAN bug that let it
set constant 1 to 0 if you passed it to a subprocedure that set its argument to 0.

After the call setzero(1) you could code
 IF 1 = 0 THEN
    PRINT("MATHEMATICS HAS FAILED!")

and the test would succeed, causing the PRINT statement to fire.

This was because that early version of FORTRAN would be blind in its call by reference.

See the attached tests to see how that is not a problem in AI2 for constants or simple variables.

  


-- 
Nice. Thanks for it.


-- 

댓글 없음:

댓글 쓰기