What is the scope of code that is outside of any function or other explicit block?

This is I think a very basic question, but I’m struggling to find documentation on it in the Lua resources, probably because this is a kind of basic principle of coding, or I don’t know the proper terms for what I’m describing, other than “code outside of an explicit block”.

So, code that is placed outside of any function, and outside any explicit block (any block ending in end or until), ie code that stays at column 1 when you indent. One thing I’ve noticed is that if you hit the restart button when the program is running, the code outside of any block is not executed again. So it seems to be once only, when you hit run. But what is the scope of local variables declared outside of any blocks? The Lua reference here says the scope of local variables declared outside of an explicit block extends to the end of the “chunk”, but having also read the section on chunks, I don’t understand where a chunk ends.

I use stuff outside of blocks for things like displayMode and supportedOrientations and declaring global variables that are needed for the names of functions (ie say if I’m wrapping a set of functions in a table that isn’t a class), ie the scaffolding if you like of the code. But I was wondering about the other implications and use-cases of placing stuff outside of any block, and particularly about local variables declared in this way.

BTW, what does the repeated double colon mean throughout the Lua reference guide? as in

stat ::= local namelist [`=´ explist]

I guess it’s a convention that is so widespread that no-one thinks they need to explain what it is. Maybe it’s because Lua is a scripting language, so the doc authors assume you must be coming to it from C# or whatever.

Local variables declared outside functions are recognised anywhere in the same code tab, but not in other tabs.

@Ignatz thank you! If you restart the program, code outside of functions doesn’t seem to be executed again, but variables you’ve declared still seem to work.

That’s probably because Codea only compiles code the first time, including the external local variables, and second time it just runs setup.

@yojimbo2000 If I understand your question, your saying any code outside a function doesn’t run when the replay icon is pressed. Here’s some code that show otherwise. As far as I know, any code outside a function gets executed before any code inside a function. Tap the replay button.

EDIT: Just in case I mixed up the “replay and restart”, here some new code that uses restart. Tap the screen for restart. All the code is executed for restart also.

a="code at start\
 outside any function"

function setup()
    a="code inside function setup()"

function draw()
    text("tap screen for restart",WIDTH/2,HEIGHT/2)

a="code at end\
 outside any function"

function touched(t)
    if t.state==BEGAN then

@dave1707 really? When I try your code, I see all 3 messages on the first run, but on every restart, I only see the code inside the function. Do you see all 3 messages on each restart?

@yojimbo2000 I see all 3 messages when I run the code, when I press the replay button, and when I tap the screen to do a restart. But then I’m also running Codea 2.2 (34).

Re scope: each tab is a separate “chunk” in lua terms. So local stuff in a tab is only available within that tab.

like @yojimbo2000 i only one message on restart (codea 2.1)

I ran the code on my iPad 1 with Codea 1.5.5 (21) and when I do a “run”, I only see the print from setup(). If I do a replay or restart, I see all 3 messages. I altered the code so that I had different variable names for the strings outside of the setup function. I added print statements in setup to print those variables. When I ran the code, setup printed all 3 strings. So the variables got assigned, but the print didn’t show.

@dave1707 Codea 2.2, cool. Are you a beta tester? I added a random number to the variable a before the setup function, and it changed with each restart. So variable declarations are re-executed on restart, although other stuff (such as print statements) are not, at least in 2.1

@yojimbo2000 Yes, I’m a beta tester. It’s nice to get the new code early. That’s what I was seeing with my iPad 1. It didn’t look like print worked, but other code executed. But with 2.2, I see all the print results.

It’s good to hear experts playing newbie tricks.

As a newbie I would follow the rules and stick to the two preloaded functions until advised otherwise. If rules are not followed then any attempt to maintain compatibility between successive upgrades of IOS, and newer versions of Codea or Lua would become more difficult.

@Steep there’s no rule against putting code outside an explicit block, and sometimes it’s essential, for instance in getting supportedOrientations to work properly

@Steep I’ve found putting code outside functions to be very useful, for example my 3D explosions, where I had some code that pre-rendered a short animation to use. (If I put it in a function, it would generate the animation multiple times, and it was way too expensive to calculate in realtime.)

@SkyTheCoder you can have code in a function, and make so that it is played only once… (with a static variable playedOnce=false that you test and set true in your function.). But i am sure you know this.

@Jmv38 Yes, but that would require a variable playedOnce that was outside any functions, wouldn’t it? (The animation’s images were local to the tab, so no global variables)

Sorry if I am drifting off topic but again, as a newbie, I must ask can what you are doing be called jailbreaking ?

@Steep Jailbreaking is when you remove the restrictions of iOS via some third-party program so you can install applications and tweaks that were not approved my the App Store. So no, nothing in Codea has to do with jailbreaking. (I personally wouldn’t recommend jailbreaking as it waivers your iDevice’s warranty, and there aren’t many super-useful advantages to jailbreaking, in fact it can be a bit dangerous because you could download a virus, or your iDevice could get bricked.)