I don't understand why this will not work. Can someone help.
PRINT "what is your name?" ,0,0
INPUT name$ ,0,5
PRINT "hello ",0,10
PRINT name$,0,15
SHOWSCREEN
MOUSEWAIT
END
the error says something about an unassigned variable
Name$ is not declared at the start of your code. You can do one of two things -
1. Make name$ global by using
GLOBAL name$
Do this at the start of your code.
2. Turn off explicit declarations by going into OPTIONS and unticking Explicit Declarations
Welcome BTW :)
PRINT "what is your name?" ,0,0
LOCAL name$
INPUT name$ ,0,5
PRINT "hello ",0,10
PRINT name$,0,15
SHOWSCREEN
MOUSEWAIT
END
thank you so much
Hi I666NoB666I,
I don't know if you are new to programming in general, or just new to GLBasic.
Assuming you may be new to programming altogether, I figure I should elaborate a bit, as Ian and I gave two similar ways to handle the same thing, but what happens in your program is actually quite different.
What is the same in both examples is that we both declared the variable before using it, so now, GLBasic knows to expect a string called name$.
The difference is GLOBAL vs LOCAL.
GLOBAL variables can be directly accessed and manipulated from anywhere inside your program, including from functions without having to directly pass the variable as an argument, or even a pointer to the variable.
LOCAL is only accessible from the scope within which it was created, so to access a LOCAL variable from a function other than which it was created, it must be manually passed.
While this may seem like extra work and an extra layer of complexity to your program and more to learn and understand, it will be important to understand as your programs grow in size and complexity.
My personal preference and recommendation is to always use LOCAL variables, unless there is good reason to use GLOBAL for a particular circumstance.
Of course experience will help you to make these kind of decisions.
This will help keep your code organized, easier to maintain, and in many cases, it will help make functions more portable to reuse from one program to the next.
Please understand though that all programmers have their own style and preferences, and others on this forum may disagree with me entirely, which is OK.
Programming is as much art as it is science, so in the end, what is important is that you achieve the results you envisioned when you decide to start a project.
Oh, and in case perhaps you already knew all this and you are experienced, I apologize if this was in any way condescending, it is certainly not intended to be.
I just wanted to explain further in case you are indeed a beginner, and I hope this is useful to you.
Anyway, best of luck with GLBasic, I am sure you will love it.
I certainly do, and BTW, I work in numerous languages, but GLBasic is at the top of my list of preferred languages.
This is especially due to Gernot's always active support, and the very friendly community here.
And as Ian said in his post, Welcome :)
regards,
Dave
EDIT - One more thing I want to mention, personally I recommend to leave explicit declarations turned on.
This will force you to be a bit more disciplined, and IMO it is good practice to always explicitly declare, as well as type (string, integer, float, etc) everything.
I'll now give my take on this.
I too prefer LOCAL variables if I know they are to be used ONLY in one function and have no need to be used by anything other than in this function. I suggested GLOBAL, as I couldn't be sure that your example wasn't going to be of use elsewhere too. Something like that may be used in several functions.eg Get name, display as part of a high score table or even save the name to disc. This is just presumption though.
I hate Explicit Declaration. The way I code, I don't need it. I document every function and comment my code very well. I also tend to use certain well named variables for the same things, no matter what my game. And I don't want to add $, % or # to my variables, as I know whether a variable is a string, integer or float. I can leave code and come back to it later and quickly know/understand what it all does. Like aonyn I've coded in lots of languages and in none of those did I use explicit declaration unless I had to. It's a presonal thing, but I do understand it's use/value.
However, I gave a solution to your problem with explicit declaration, not a suggestion. It's upto you to use it if you feel you need to.
Out of interest (I'm in work so can't test), but what happens if you have a GLOBAL and a LOCAL with the same name? I use LOCAL for most things, but sometimes if I copy a sub from these forums to test with my code then it is a bit of a worry that a LOCAL may muck around with my GLOBAL if the same name. Is GLB clever enough to override a GLOBAL with a LOCAL in a subroutine/function if setup with the same name? Would be good if it is, then can drop in a pre-made sub that is competely cut off and does it's own thing and not worry about if variables are the same name or not as my GLOBALs.
And if it is clever enough then is there a way to access the global still? So, for example, you import a complete routine leaving it to itself. Then later you realise it shares a LOCAL with an old GLOBAL and you would like to use it in the routine, but really don't want to change the routine, or your own code. So, can you do something like GLOBAL.MYVAR$ for example to reference it? Would be quite nice if you could do.
Cheers
If you use a LOCAL variable (of the same name) after defining the GOBAl variable, the LOCAL variable over-rides the GLOBAL variable.
So if you use -
GOBAL name$="TOM"
...
...
...
LOCAL name$="JERRY"
PRINT name$,10,10
Once you run your code name$ will be printed as "JERRY"
You can, however access the global with:
PRINT GLOBAL name$, 0,0
It's all in the manual, but it's very bad style.
Ooh, cool. I thought it might, just never tried it, or even thought about it until this point.
Manual? I thought it was the unwritten programmers rulebook to never look at the manual? Wait, that might be DIY... :)
Cheers
Ps. I lie. Everytime I open the 510 page PDF manual for my main Unix programming language that I've used almost every day for the last 16 years then a little part of me dies :(
Could always buy my books :)
Then more parts of me will die! :) Heh, your main problem is that it's in a BASIC syntax, and for a lot of us older lads that means most of it is second nature. Especially if you come from a Blitz/DB/VB/Div gaming background.
Cheers
Hi Ian,
I just want to apologize, I hope I did not offend you by implying your way was wrong. That was not intended, and I am sorry if it came across that way.
This was a piece of advice which was passed on to me when I was at the very bottom of the learning curve, and which I have personally found to be one of the most useful pieces of advice I have been given. Overall, the method I described keeps me disciplined, and helps me to keep my code organized. Certainly though, nothing was wrong with the way you described.
I just wanted to clarify the difference between the two ways, for a new member who I presumed is also a new programmer, and pass the advice which was beneficial to me when I was starting.
As I said in my first post though, I see it as much art as science, and I expected that others would see the matter differently.
I am sorry to have come across as demeaning your post.
regards,
Dave
Absolutely no need to apologise :)
I never took your comment as anything like being an attack. No worries. TBH your approach is definitely better suited to someone that hasn't picked up bad habits like mine. My response agreed that LOCAL commands are better and that I use them myself. I only stated GLOBAL usage because it is simpler to understand one way of defining variables than having to understand two, plus he might want to use that variable elsewhere.
As for the Explicit declarations, I also stated that it is optional, and not a suggestion. And implied that I'm just a lazy git.
So there you are, no need to apologise. There are a million ways to come up with the same answer in code - no one way is the right way, but there are sometime better and quicker ways to achieve the same outcome.
Better to have two responses/explanations/possible ways of achieving an outcome than none :)
Thanks Ian :)
TBH as well, you mention you comment your code very well.
Personally I comment only where things need commenting, and rather rely on very explicit code, as well as the way I structure my code and names I use to make my code as "self documenting" as possible. This works for me, but I'd put it in category of bad habits I would never recommend to someone who is beginning. My code as a result is often quite a bit more verbose than most programmers would prefer. :)
regards,
Dave
As it happens our old QA department would fail our code even if it worked perfectly if the code wasn't annotated to an appropriate level. Some people over did it though and practically repeated the code in the annotation. Eg. READ (AFBOOK,KEY=ID$) B1$ ; REM "Read from booking file with ID into B1$
Nice balance is always good!
Cheers
No matter how much you comment or make your code readable Eagleson's Law
will still apply :)
"Any code of your own that you haven't looked at for six or more months
might as well have been written by someone else."
although it's about 2 weeks in my case ;/
Don't forget about Static Variables. If you need the variable to be remembered the next loop, but don't want to use a Global variable. Try out Static, its like a Local, only the information is saved.
Like global variables, static ones are The Work Of The Devil :)
QuoteAny code of your own that you haven't looked at for six or more months
might as well have been written by someone else
Heh, so true! Sometimes I don't even recognise my own code from a few weeks ago. Even more worrying is the system I've used at work for 16 years has something like 6500 individual programs (original limit of a program back in 1989 was 65k for the langauge so we used a lot of modular design back in the day). I've probably modified half of them at least (esp. after Y2K update where I did over 2000), with mods and bug fixes, and wrote I guess about a thousand of them. I have to admit on more than one instance I have been angry over some idiots code from a decade or so back (to be fair we must have had well over a hundred programs come and go), and then slowly realise it was actually me once I check the original jobs release details. Doh. Keep that one to myself...
Cheers