05-21-2025, 08:40 PM
(05-21-2025, 12:11 PM)mdijkens Wrote: Example showing difference passing parameter variables by reference or by value:Good example but it can bring some confusion:
Code: (Select All)
x = 5: x$ = "ABC"
Print "By Reference:"
Print "Checkfunction returns "; CheckFunction$(x, x$)
Print "After CheckFunction x="; x
Print "After CheckFunction x$="; x$
x = 5: x$ = "ABC"
Print "By Value:"
Print "Checkfunction returns "; CheckFunction$((x), "a" + x$)
Print "After CheckFunction x="; x
Print "After CheckFunction x$="; x$
Function CheckFunction$ (x, x$)
x = x * 3: x$ = x$ + "DEF"
CheckFunction$ = "hello"
End Function
I prefer to show variables out and into procedure...
Code: (Select All)
x = 5: x$ = "ABC"
Print "By Reference:"
Print "Checkfunction returns "; CheckFunction$(x, x$)
Print
Print "After CheckFunction x="; x
Print "After CheckFunction x$="; x$
x = 5: x$ = "ABC"
Print "By Value:"
Print "Checkfunction returns "; CheckFunction$((x), "a" + x$)
Print
Print "After CheckFunction x="; x
Print "After CheckFunction x$="; x$
Function CheckFunction$ (x, x$)
Xp = CsrLin: Yp = Pos(0)
Locate Xp + 1, 1
x = x * 3: x$ = x$ + "DEF"
Print "in Function: x = "; x; "X$= "; x$
Locate Xp, Yp
CheckFunction$ = "hello"
End Function
but it brings a hidden issue...
What issue?
the second parameter of the Function is named X$ like the global variable X$ but their are 2 different variables... in the execution the compiler uses a different variable into function to work with a string... so when you modify X$ in Function you modify that is not the global X$ but "a"+X$... so when the value is popped out at the end of the procedure it has not been put into global X$ because it is "a"+X$. If you delete the string "a" added to X$ in the calling of function you'll observe that the global X$ has been changed.
And so?
The correct way to pass string for value is (X$) and not X$...you get global X$ unchanged because you pass more than X$ as parameter, so the changement cannot be put into global X$ after exiting Function.
The confounding issue is to use the same name for parameters of FUNCTION and the global variables. We must use only the same type of variable, not the same name.
Thanks for focusing on this aspect of passing parameter by reference. A composite value passed have no return horse.
here your code modified to show better the issue:
Code: (Select All)
Color 0, 3
x = 5: x$ = "ABC"
Print "By Reference:"
Print "Before CheckFunction x="; x
Print "Before CheckFunction x$="; x$
Print "Checkfunction returns "; CheckFunction$(x, x$)
Print
Print "After CheckFunction x="; x
Print "After CheckFunction x$="; x$
Color 9, 2
x = 5: x$ = "ABC": Y$ = "000"
Print "By Value:"
Print "Before CheckFunction x="; x
Print "Before CheckFunction x$="; x$
Print "Before CheckFunction Y$="; Y$
Print "Checkfunction returns "; CheckFunction$((x), Y$ + x$)
Print
Print "After CheckFunction x="; x
Print "After CheckFunction x$="; x$
Print "After CheckFunction Y$="; Y$
End
Function CheckFunction$ (x, x2$)
Xp = CsrLin: Yp = Pos(0)
Locate Xp + 1, 1
x = x * 3: x2$ = x2$ + "DEF"
Print "in Function: x = "; x; "X2$= "; x2$
Locate Xp, Yp
CheckFunction$ = "hello"
End Function
here screenshot of output
![[Image: immagine-2025-05-21-223807334.png]](https://i.ibb.co/Dgs8cw1M/immagine-2025-05-21-223807334.png)