Notice that the function uses LOCAL values for all of the variables named in its definition, and GLOBAL values for all other variables. When the function evaluates A+B+Z, it uses the value 2 (passed in) for A, 3 (passed in as the value of Y) for B, and 20 (the global value) for Z. So when we return, A (our output variable in line 3) is 2+3+20=25.
Our global variable B is masked off when we call FUNC, and so it's not affected by line 8, so after FUNC is called, it's still 10. Likewise, our global variable X is masked off when FUNC uses X for its return value, so it's still 7 after FUNC is called.
When Y is passed in as the value of B for a call to FUNC, it's passed by VALUE rather than REFERENCE. So when we change the local value B in line 8, the global value of Y is unaffected. After the call to FUNC, Y is still 3.
Since Z isn't listed in the definition for FUNC, it isn't masked off in any way. Therefore, when we use its value in line 7 and change it in line 9, we use it as a regular global variable, and it IS altered to 22 after the call to FUNC.
This is all rather confusing, but hopefully it makes at least some sense and shows you how careful you have to be about the variables you use in your functions.
When I saw this, I was about to ask if there was a way to pass by reference or use addresses since I just did this a week ago for a data structures class. XD
That said, are there pointers in this?
Pointers? I could have sworn there was a way to refer to a variable named in a string (for example, if A$="X", it could be used to refer to the variable X), but I've been through all of the instructions, and I can't find it. I might be thinking of a different language.
Oh, I'm thinking in C/C++ since that's what language my Data Structures course is in. I never knew you could use an array for this. I know in Java, there's only pass by value.
Can you access the heap in Smile Basic?
It's been a while since I've done data structures, but I know Smile BASIC has Push, Pop, and Shift functionality in its arrays.
You can see the complete instruction set online at smilebasic.com and the instructions relating to data structures in particular are here: http://smilebasic.com/en/reference/#variable