プレイ日記
Ed CPFace
Here's an illustration of how Smile BASIC handles scoping in its user-defined functions. What will FUNC do to our variables here?
4そうだね
プレイ済み
返信[1]
親投稿
Ed CPFace
Here's the output, A, B, X, Y, and Z.
0そうだね
プレイ済み
返信[2]
親投稿
Ed CPFace
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.
0そうだね
プレイ済み
返信[3]
親投稿
Ed CPFace
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.
0そうだね
プレイ済み
返信[4]
親投稿
Ed CPFace
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.
0そうだね
プレイ済み
返信[5]
親投稿
Ed CPFace
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.
0そうだね
プレイ済み
返信[6]
親投稿
appletart NoName-sami
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?
0そうだね
未プレイ
返信[7]
親投稿
Ed CPFace
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.
0そうだね
プレイ済み
返信[8]
親投稿
れい rei-nntnd
you can use array instead of reference.
1そうだね
未プレイ
返信[9]
親投稿
appletart NoName-sami
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?
0そうだね
未プレイ
返信[10]
親投稿
Ed CPFace
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
0そうだね
プレイ済み
返信[11]
親投稿
appletart NoName-sami
Oh awesome! Thanks! :D I'm still new to programming, so this'll definitely come in handy!
1そうだね
未プレイ
返信[12]
親投稿
れい rei-nntnd
no, we cant access heap,stack,... , directly.
1そうだね
未プレイ
返信[13]
親投稿
Frood Volkanon
If you want Z to be a local variable, you can declare it with VAR.
0そうだね
プレイ済み
返信[14]
親投稿
Ed CPFace
Thanks Frood! That's super helpful.
0そうだね
プレイ済み