# Using the 3D Connexion SDK/Procedures
### So, What are Procedures?

A procedure is a bit of code which you can move out of your main program and use again and again for different purposes. Let's say you had a program which tool in variable A, and divided it by 2, squared the result and added 4, and did this 10 times. In standard Visual Basic, it would look like this:
If you do this a lot, it makes sense to get Visual Basic to do all the tedious coding. Once you've read this chapter, you'll be able to make a procedure which does it for you. If you called it 'Calculate', your code could look like this:
This causes the program to jump to 'Calculate' and then jump back when Calculate is finished.

### Making a simple procedure

To make a procedure, let's make a new Visual Basic program and add a button. Add a Click event to it (Look at the Hello World Tutorial for more). Declare an Integer variable called MyNumber:
In visual basic, our entire program is just one massive procedure for Windows (or other OS) to run, so procedures inside the program are called Sub-Procedures, or Subs for short. Visual basic lets us say whether it is Public (other programs can start it running) or Private (it is invisible to other processes). We'll make ours public. You can see in the code that Visual Basic has already made a Private Sub for our button click event. Let's add another sub, this time Public, and call it 'DoubleMyNumber':
The reason for putting the '' after the name of the procedure will become clear in a moment. (Visual Basic actually adds it in automatically)
So, let's add the code to DoubleMyNumber:
We need to call DoubleMyNumber when the button is pressed. This is nice and simple; we just type its name:
Congratulations; you've just made a new procedure.

### Adding Parameters

To make the procedure more useful, we can add parameters. A parameter is a variable which you can send to a procedure. Let's say we need to multiply the number by something other than 2. How about being able to do:
We can tell the procedure to expect a variable. In fact, we can tell it to expect as many variables as we like, and separate them with commas. We could have a procedure to add 5 numbers:
All we need to do is declare it when we write the procedure description. Let's edit the program we just made. The brackets after the name are actually the place where a parameter can be declared. First we have to tell the procedure what to do with the variable after running:
ByRef: Call by Reference. This means you can change the variable
ByVal: Call by Value. The procedure cannot change the variable
Let's use ByRef. At this level, the choice of ByRef and ByVal is rarely important.
The code to get the parameter ByHowMuch is like so:
Here's the complete code:

### Functions

A function is like a procedure, but it is actually a variable. Let me explain:
Half(14) can be used just like a number. In fact, it is a number. Functions are much more flexible than procedures because they can be used to assign to different variables:
Like procedures, functions can have more than one parameter:
We have to declare functions in a different way:
Look at the last 2 words of line 1; As Integer. We have actually declared DoubleMyNumber as a new Integer, which we can assign to. We say DoubleMyNumber(Mynumber) = MyNumber * 2, and visual basic can work out that DoubleMyNumber(7) = 7 * 2
Visual Basic has a huge number of built in libraries of functions, try typing 'Math.' to open the Mathematical Operators library.

### Welcome to the VB World!

If you read and understood all that, you're now ready for the big bad world of Visual Basic. You're also ready to get going on the 3D connexion SDK. If you didn't understand it, you need to hunt around for a Visual Basic course which suits you. There are loads out there, mainly much better than this, so happy hunting!
Go to the contents and start on the 3D Connexion tutorials.
* Home