String Variables

Previous topic - Next topic

I666NoB666I

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


Ian Price

Name$ is not declared at the start of your code. You can do one of two things -

1. Make name$ global by using

Code (glbasic) Select

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 :)
I came. I saw. I played.

aonyn

Code (glbasic) Select

PRINT "what is your name?" ,0,0
LOCAL name$
INPUT name$ ,0,5
PRINT "hello ",0,10
PRINT name$,0,15
SHOWSCREEN
MOUSEWAIT
END
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

I666NoB666I

thank you so much

aonyn

#4
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.
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

Ian Price

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.
I came. I saw. I played.

Crivens

#6
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
Current fave quote: Cause you like musicians and I like people with boobs.

Ian Price

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 -
Code (glbasic) Select

GOBAL name$="TOM"
...
...
...
LOCAL name$="JERRY"

PRINT name$,10,10


Once you run your code name$ will be printed as "JERRY"
I came. I saw. I played.

Kitty Hello

You can, however access the global with:
Code (glbasic) Select

PRINT GLOBAL name$, 0,0

It's all in the manual, but it's very bad style.

Crivens

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 :(
Current fave quote: Cause you like musicians and I like people with boobs.

MrTAToad

Could always buy my books :)

Crivens

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
Current fave quote: Cause you like musicians and I like people with boobs.

aonyn

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
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

Ian Price

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 :)
I came. I saw. I played.

aonyn

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
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9