October 29, 2025, 06:57:42 AM

News:

IWBasic runs in Windows 11!


global variable

Started by kryton9, July 19, 2006, 08:49:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kryton9

This compiles with no error, but the global variable doesn't work:
def g as int;
global g;
g=23;

global sub main()
{

print(g);
while getkey() ="";
}

I tried def g as global int; and I get compile errors.
def g as global; also gave an error.

Zen

This will work aslong as they are both in the same source file...

int g = 23;
int a = 0;

global sub main() {

a = 10;

print(g+a);
while getkey() ="";

}


Lewis

Ionic Wind Support Team


global g;
int g = 23;


You almost had it right.

'global' when used in this context tells the assembler that there will be a variable named 'g' that should be in the program global address space. It doesn't have to exist before the statement.

int g = 23;

Is a global initializer,when refering to a non local variable.  The compiler reserves the space for it and tells the assember to initialize the value to 23.  No code is generated since it is automatic.

g = 23;

Is a runtime assignment. If it is not in the path of execution then it does nothing outside of subroutine scope.  Code is generated but there is no way for the program to get to it.

Paul.
Ionic Wind Support Team

kryton9

Thanks, really going through the tutorials in detail to get up to speed. I remember you had a post that covered classes too, couldn't find it and also had made recommendations for reading some sites on the web to understand classes and object oriented programming. Will get to those by tomorrow I hope by the time I get the basics down. And then on to 3d. Really psyched by all you developed so quiclkly, you are amazing!!!

Zen

I think once you find out how OOP works and can just get your head around how it does you will be fine. I think a lot of people get scared about going from procedural to OOP because its quite different, but when it comes down to it, it is so much easier, cleaner and saves a lot of code re-writing too.

Lewis

kryton9

Thanks Zen, I believe what you are saying and so I am taking the leap into it finally.