In the previous couple of lessons, we looked at how to use BASIC to work with text and numbers, or "strings" and "integars" as they are officially callled. We even looked at how we can use BASIC to produce random numbers.
In this lesson, we'll look at how you can work with a string or an integar without even knowing what it is yet.
Honestly! This can done by replacing the string (or integar) with a placeholder while we write the code. We won't know what the value of that string or integar is until we run the programme.
Think about it this way. Have you ever played a board game where one of the pieces was lost long ago, and you had to use something else to represent it? This happened to me during Pokemon cards but it was so subtle I didn't think anyone would notice:
That green sticky note could have been anything, but I needed it to be a Venosaur as I'd lost mine. Using placeholders in programming is a bit like that, except it's not the cardinal sin that it is in Pokemon cards.
Let me know you what I mean. If you haven't already, open up the BASIC console and start a new programme:
The programme we'll be making is going to be a robot with a slight personality problem. It will be called the Passive Aggressive Robot.
Try typing this as your first line and hitting ENTER, except you can use your own name instead of "Jonny":
Then add these lines:
Notice how we are using single quote marks when quoting the newsreader. This is because we want these quote marks to actually appear in the text when we run the progrmame. Trying to use double quotes in the text would have confused BASIC because it would have made it think we were ending the string that was being PRINTed. So becuase BASIC uses double quotes to indicate a string, if we want a quote mark to actually appear IN a astring, we have to use a single quote.
Now try RUNning this programme:
And it should work nicely like this:
That's a pretty funny programme!
But now think about this: imagine we decided to change the name. Say we didn't like the name Jonny, or your name, and wanted to put someone else's name in there for the robot to talk to. What then?
We could rewrite all the lines that contain the name to the new name. That would work. But what if we wanted to change the name again after that? We'd have to do the same thing again.
BASIC, and all programming in general, has a solution for this. We can use a placeholder. Take a look:
To start with, we will need to add a new line to the beginning of our programme. Our first line is line 1, so for now, let's make a line 0 with this code:
And don't forget to hit ENTER.
At this point, you might rightly be thinking that it's crazy how restrictive BASIC seems to be with the line numbering. We wanted to put a new line in that would run before line 1, so we made a line 0. Well that's all well and good but what if we want to put a line in before that? Do we need to make a line -1?
Don't worry about this for now. BASIC does have a feature that will allow you to insert lines before other lines, and we will get to it, but it has nothing to do with placeholders so we'll skip it for now. We'll come back to it later I promise.
What we've done here is create a placeholder called NAME$. Yes, name and dollar sign. Let's talk about this strange naming method for a second.
This is telling BASIC to that we are making a placeholder which we'll refer to as NAME$. The reason there is a dollar sign at the end is to tell BASIC that the placeholder will be a string. We then set the value of that string to "JONNY" (or your name).
What's important to know for now is that what we call the placeholder doesn't matter, as long as it ends in a dollar sign. We've called ours NAME$ because it will always represent a name, like yours or mine, so calling it "NAME$" makes it easier for us to understand what the placeholder represents.
But we could have called it anything, like "SIGN$", "LABEL$", "ALIAS$", "TROUSERS$", "BLOBBY$", "POKEMON$", "NINTENDO$", whatever. It would still work just as well as BASIC really doesn't care what it's called. What's important is that it makes sense to you, otherwise you can end up confusing yourself:
Now that we have a plaeholder called NAME$ being made on our first line of the programme (line 0 at the moment), we can rewrite line 1 of the programme like this. Give this a go:
And hit ENTER. Can you see what we've done here? We've removed the name from our original string, so it's now just HELLO with a space after it, and then we put in a plus sign, and then we put in our placeholder
Try RUNning the programme now. You will get this:
So... nothing's happened. Everything is still the same. We haven't achieved anything... right?
Except... try this. Try changing line 0 so it uses a different name. Pick any name you like. I'll go for Wendy:
Hit ENTER and then RUN the programme again:
And you get this:
Now look at that! The programme says hello to the new name! That's because that line (line 1 of the programme) is now using the placeholder. Does that make sense? If not, simply run LIST and take a good look at line 1 until it makes more sense.
The other lines of the programme still say the old name, Jonny in my case, because they haven't been changed to use the new placeholder. So let's do those now.
We've already done line 1, so let's do the other lines that include the name. There's six other lines that need changing, give this a go:
Watch out for line 7! That now starts with one single quote surrounded by double quotes, so the name can be added after. I can see why that would be confusing. It was confusing for me and I do this for a living, so watch out.
Let's take a look at the whole programme now that we've changed those lines. Try LIST and you should get this:
So now every occurance of the original name has been replaced with the placeholder. So try running the programme and you should get this:
And look at that! Now we have a version of the programme where we can change the name really easily! Give it a go, change the name to something else by changing line 0:
And RUN the programme again:
Hopefully now you can see the power of placeholders. It allows use to work with a string (or integer - we'll look at that later) before we even know what it's been set to yet.
Now that we've learned how to use the placeholder, let's take a look at a another problem that we've been running into whilst working on our BASIC applications so far.
When we decided earlier that we wanted to add a new first line to our programme:
We had to number it line 0 in order to force it to be run before line 1. You probably thought this was a bit weird. Sure, that technically works, but what if we wanted to add another new line to the start? Would we need a line -1?
And what would happen if we wanted to add a new line between two other lines. Say we wanted to add an extra comment between lines 11 and 12 to make the programme funnier:
How would we do that? Would we have to number it line "11 and a half"" or something? Let's take a look at this now.
Told you we'd come back to this ;)
This was a common problem for programers of BASIC. If every line has a number, what happens if you want to add a new line between two current lines?
To get around this, the makers of BASIC added a command called RENUMBER. Try this:
And hit ENTER.
This command is a bit like LIST or NEW in that it is never part of a programme itself, but a tool to help you make your programme.
You can see what this has done by running LIST again:
Can you see what's changed? Everything is the same except the line numbers now start from 10, and go up by 10.
That's how RENUMBER works. When you use RENUMBER, you have to give it two numbers with a comma between. The first number is the line number that you want your programme to start on, the second is what you want it to go up by:
How does this help us? Because now we have a very easy way of adding our new line. Instead of trying to get a new line between lines 11 and 12, we now want to get a new line between lines 120 and 130.
Which is much easier. We can just make a line 125. So try this:
And hit ENTER. If that went ok, run LIST, and you should now get this:
Now try RUNNING the programme, and you should get this:
There, now it's much funnier!
What most BASIC programmers would do at this point it run RENUMBER again. This is to make it easier to add new lines later on, so you don't have to worry about whether our new line needs to be before 125 or after. So lets do that (and hit ENTER):
Now LIST the programme and the lines should all go up by 10 again, making it easier to add even more lines in future:
In fact this is, in reality, how programmers use to work with BASIC. They always did the lines starting from 10 and going up by 10, so they could add extra lines later and then tidy them up with RENUMBER. We just didn't do it at the start of this course because we were worried it would be too confusing. But from now on we're going to do our line numbers like this.
Now that's a damn good programme with some very tidy line numbers! But there is something else we can do to add the cherry on top...
What we have so far is hilarious. But there is one last thing we can do to make it even better, and it uses one of BASIC's coolest features.
We can change this programme so that the user types their name in while it's running.
This will be the final piece of the puzzle, as it means we won't need to change the name within the programme anymore. When this is done, we won't have to change the code ever again.
And the change is actually quite simple. We just need to use a new command called "INPUT". Try this: (and as always, don't forget to hit ENTER)
What we have done here is change line 10. Line 10 was originally this:
We've changed it so that the line is still going to be setting the value of the NAME$ placeholder, but it's going to do it by asking for input from the user. Hence the use of the word "input"
What will this actually look like when we RUN it? Patience! We need to add another line first so it all makes more sense.
If we're going to get the user to type in their name, we need to tell them so. Otherwise they won't know. So let's add another line at the start. We can choose any number from 0-9 to add a line at the start, so lets go for 5. Try this (don't forget to hit ENTER!):
Lets tidy up after ourselves now and RENUMBER the lines:
So now check your programme with LIST and it should look like this:
That looks good to me! Well it's time. Try RUNning it:
Why what is this we have here? A question mark???
Notice what you can do now... you can type something in WHILE THE PROGRAMME IS RUNNING! What sorery is this? So let's try it:
And hit ENTER.
What do you get?:
LOOK AT THAT! We're now setting the name from user INPUT. Go ahead and run the programme a couple more times and try some different names. Have fun with it. You deserve it!
Notice how whatever name you type in, it's then used throughout the whole programme. That is the value of a placeholder. You can write an entire programme using a placeholder and then ask the user to give you its value when the programme actually runs!
In fact, this programme works so well that I managed to catch Si out with it shortly after designing this programme for the lesson:
Now that is the power of good programming.
Pssst! You've just spent a lot of time writing this programme. You might be worried about going on to the next section and having to start a new one, and losing this one.
Don't worry, you can save any programme you write in BASIC. We'll talk more about this later, but for now just know that if you want to save the programme, just type SAVE and hit enter. It will download your programme as a text file.
We'll tell you what to do with that file later, for now just tuck it away somewhere safe!
Oh would you look at that, it's Lingo Time again. Have a guess on the question below to see if you can win another one of Si's amazing prizes: