プレイ日記
Ed CPFace
Tip of the day! Label scope. The more structured your program is, the less need you'll have for labels. But what happens if you need a label in a function and you accidentally reuse a label from your main program?
8そうだね
プレイ済み
返信[1]
親投稿
Ed CPFace
SmileBASIC doesn't mind! Labels defined within a function are considered local to that function, and do not count as duplicates if the same label is used elsewhere.
2そうだね
プレイ済み
返信[2]
親投稿
Ed CPFace
Moreover, GOTO cannot be used to travel between functions. If you try to GOTO a label defined inside a function from your main program, or vice versa, the program will throw an undefined label error.
2そうだね
プレイ済み
返信[3]
親投稿
Ed CPFace
RESTORE works a bit differently. You CAN reference global labels from inside a function; this example lets you read the 5 just fine.
2そうだね
プレイ済み
返信[4]
親投稿
Ed CPFace
However, this won't work. You can't use RESTORE at the global level to get at a function's data; this code throws an error.
2そうだね
プレイ済み
返信[5]
親投稿
Ed CPFace
What about this? A RESTORE inside a function will favor a local label over a global one. In this example, we read the 2, not the 5.
3そうだね
プレイ済み
返信[6]
親投稿
Ed CPFace
What about this?! Actually, it works just fine. Once you've set the data position with RESTORE, you can READ that data from anywhere else in the program.
3そうだね
プレイ済み
返信[7]
親投稿
Ed CPFace
There's probably not going to be many reasons to structure your program in such a confusing way to begin with, but it never hurts to know the rules.
4そうだね
プレイ済み
返信[8]
親投稿
Cris cmart592
Throwing so much stuff at use, we are dying.
1そうだね
プレイ済み
返信[9]
親投稿
BBB agaaron
Very good information about scope in functions. Thank you much Ed!
1そうだね
プレイ済み