LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

COMMON DEF

Root / Programming Questions / [.]

MinxrodCreated:
Can variables in a COMMON DEF statement be from both the slot defined in and the slot executed in? Like you have a DEF in slot 1 and call it from slot 0, could it use variables defined in both slots?

A variable declared within a DEF block is absolutely invisible to any code that is not in that same DEF block, whether you use COMMON or not, whether you're in the same program slot or not.

You can access variables from outside a COMMON DEF statement but only in the same slot. You can only use variables in the slot they were defined.
A variable declared within a DEF block is absolutely invisible to any code that is not in that same DEF block, whether you use COMMON or not, whether you're in the same program slot or not.
This is true, but you have to explicitly declare a variable with VAR or DIM for it to be local to the DEF to END section. The variables used to receive argument are automatically defined as local.


Also, check out this post. https://www.smilebasicsource.com/forum?fpid=5460#post_5460
I looked at it. This pretty much solved all the problems I'm having so thanks everyone for the help :) I have another question though... Can arrays be used as arguments in DEFs?

I have another question though... Can arrays be used as arguments in DEFs?
Absolutely!

I have another question though... Can arrays be used as arguments in DEFs?
Absolutely!
I have a question myself about that. You can use an array in place of a variable and vice versa when calling the function you created. Is there a way to only accept a variable or only an array?

Function parameters aren't checked for type. I discovered this when making a generic timing function, which calls another function and times how long it takes to run. You can test it out easily:
COMMON DEF MYFUNC A$,B%,C#
?A$*2, B%, C#
END
MYFUNC 2, 3.1415, "TEST"
You might expect that to fail because the parameters should have type, but it will work without errors. For this reason, you could theoretically pass an array or other variable, but in practice, with no way of reliably checking the type, you'll get errors when accessing it.

Function parameters aren't checked for type. I discovered this when making a generic timing function, which calls another function and times how long it takes to run. You can test it out easily:
COMMON DEF MYFUNC A$,B%,C#
?A$*2, B%, C#
END
MYFUNC 2, 3.1415, "TEST"
You might expect that to fail because the parameters should have type, but it will work without errors. For this reason, you could theoretically pass an array or other variable, but in practice, with no way of reliably checking the type, you'll get errors when accessing it.
Wow, that's interesting :D Thanks for all the help from everyone by the way

Function parameters aren't checked for type. I discovered this when making a generic timing function, which calls another function and times how long it takes to run. You can test it out easily:
COMMON DEF MYFUNC A$,B%,C#
?A$*2, B%, C#
END
MYFUNC 2, 3.1415, "TEST"
You might expect that to fail because the parameters should have type, but it will work without errors. For this reason, you could theoretically pass an array or other variable, but in practice, with no way of reliably checking the type, you'll get errors when accessing it.
Well that's crazy. I also noticed that you can't define two functions with the same name.
Thanks for all the help from everyone by the way
You're welcome!