Time for part 2 in our basics tour. This time we are going to talk about Procedures. Procedures will allow you to create separate blocks of code that perform a specific task. Let’s say you need to perform certain tasks often. Instead of writing the same code (a smart person would copy and paste it of course) every time you could write it once as a Procedure and just call upon that Procedure whenever you need it. Another benefit is that your code will be more easy to maintain and read. Let’s take our example from our first tutorial and turn it in to a Procedure.
Print “We left the loop!”
As you can see we only added “Procedure XPRINTING” and “End Proc” to our code. The first line gave our Procedure a name and the second will indicate the end of the Procedure. Just having this in your code will not actually execute it. You need to call the Procedure in order for it to execute. We call it by just mention it’s name:
Another way of calling it is:
The same rules that apply for the name of a variable apply for the name of a Procedure. Another near thing about Procedures is that you can close them so only the name will show but not all the code inside. Make sure the cursor is on the Procedure line and press the F9 key to fold/unfold it. You can also select the [Procedures] option from the [Editor] menu and trigger the [Open/Close] option.
I hate to do this but now I am going to get a bit technical and even maybe scare you. The variable X (X=0) in our example without being wrapped neatly in a Procedure is different than being wrapped in the Procedure. Outside the Procedure we call X a global Variable and inside the Procedure a local variable. Let’s say you change X inside the Procedure it would not effect Variable X that is outside the Procedure and the other way around. You can of course imagine a scenario in which you would like to use X in a way that if you change it outside the Procedure X inside the Procedure X will also change and the other way around. This is possible but for now I will leave it at this and discuss it in another tutorial.
Let’s use our new Procedure to learn something new. Have a look at the below code.
Procedure XPRINTING X=0 While X<11 Inc X Print X Wend Print "We left the loop!" End Proc Print "Click the left mouse button to execute the Procedure" Print "Click the right mouse button to exit" Do M=Mouse Click If M=1 Then Proc XPRINTING If M=2 Then Exit Loop Print "We have left our program"
I pressed the left mouse button first to execute our Procedure and after this press the right mouse button to exit our program. The below screenshot will show you how it will look like.
The newly added code (we know what the two PRINT statements do) is the Do – Loop part. If you understood the While – Wend control structure you will also understand the Do – Loop control structure. The Do – Loop control structure will repair what is inside it’s structure forever. We can leave the Do – Loop control structure by using the “Exit” command which you of course already spotted. It is actually really simple what we are doing in the D0 – Loop control structure. We have it check for a left and right mouse click and for each click it will do something. The left click will execute the Procedure and the right click will exit the Do – Loop control structure.
The “M=Mouse Click” command will check if the left or right mousse button was clicked.
It will store the value in M. There are three values as we can see below.
Bit 1 Single test for left mouse button
Bit 2 Single test for right mouse button
Bit 3 Single test for third mouse button, if available
All we need to do is check the value of M and execute the corresponding action. This is what we do with
If M=1 Then Proc XPRINTING If M=2 Then Exit
If M has the value of 1 which is the left click we will execute our Procedure. If M has the value of 2 which is the right click we will exit our Do-Loop control structure and with it end our program. I hope everything is clear enough to understand it. That’s it for this time and I will see you next time.