JMA201-01
Fundamentals of Application Development

DEPARTMENT SCHEDULE SYLLABUS RESOURCES
Subroutines and Functions

So far we have simply been writing statements into one event handler. This is rather limiting. What we need to do is set up some some code to be executed when we need it. We can create a block of code that will only be run when we call it by name. This lets us write code that is organized (so we can troubleshoot smaller sections at a time), reusable, and more flexible.

Imagine this - we just worked with the random number generator. We coded a formula into our program, and it worked; however, if we wanted to reuse this code to create a second random number in the program, we'd have to copy and paste. In addition to causing longer code, imagine we wanted to change our formula... we'd have to find and change both segments of the random number code. Chances are we might forget one, or change them in different ways.

Functions and subroutines solve this problem. Both of these structures allow us to group a series of statements together and give them a name. Once we have done this, we can execute those statements by name (sometimes called a function or sub call.) We don't need to insert this long block of code where we want to use it in our program - we just refer to its name. This has several advantages: first, our code is smaller - we have less repeated information. Second, it lets us write a possibly complex sub-program once - then we are free to use it anywhere in our code. Third, it means if we need to change the function, we only need to do it once - we won't have to make changes to every block of code that appeared in our program as we would if we didn't use functions.

Lets create our first function.

    Private Function addIt(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Dim answer As Integer
answer = num1 + num2
Return answer
End Function

There we are! We have created a function. First, we use the keyword "Function", which tells VB we are about to declare a function. Next, we give a meaningful function name, making sure it follows the rules of object naming. Next comes the parentheses - these indicate potential parameters, more in a second about this... Then we do a series of statements, and the function ends at the end function statement.

To use our function (again, often referred to as calling a function) we simply refer to it by name in our code. For example:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 		MsgBox(addIt(1, 2))
 End Sub

Now, what are those parentheses for after the function name, both when I create the function and when I call it?

Parameters/Arguments

Functions are a good idea - they let us break up code and use it efficiently. A function should be written to do some action, but accept different pieces of data to work on.

These pieces of data are called arguments - arguments are values passed to a function or subroutine. The values get placed into parameters - variables that exist within a function or sub (the blanks into which the arguments are placed).

Inside the function body, the parameters simply act as variables. The parentheses after a sub definition indicate a parameter list. Arguments are passed into paramaters in the order they are listed. In our example above, 1 goes into num1 and 2 goes into num2. These are local variables - variables available within the function only. (More on this in a second...)

Lets go ahead and call our function as we wrote the function call above:

addIt(4,6)

The result would be 10. If the function definition started with this line:

Private Function addIt(ByVal num1 As Integer, ByVal num2 As Integer) As Integer...

then num1 and num2 would be parameters, and 4 and 6 would be the arguments in the call.

Returning values

To return a value, use the keyword return within the body of the function. (Perhaps sometimes I want to msgbox the answer, but other times I want to use that value in another calculation. If the display is built into the function, I can't reuse it - I'd have to write another function.) Instead, we should send an answer outside of the function, and display it somewhere outside of the function definition. Here's an example:

In the example above, we sent the variable answer back out of the function call. Imagine this function call:

x=addIt(2,4)+3

The program would realize that addIt had to be simplified, and would go off and do what it needed to do. It would substitute the answer into the statement (similar to our order of operations discussion) and continue solving; during the second pass of the statement, it would be solving:

x=6+3

The result? x would be 9.

Scope - Local vs Global variables

One last thing we need to talk about with functions. We have what are known as local variables and global variables. When a function is run, it runs and then is removed from memory. Each time it is run, its code is evaluated again from scratch. Any variables created within the function are available to the function only, and only for that call of the function - they are unavailable to the code outside of the function, and they are unavailable to the function the next time the function is run. These are called local variables; the concept is variable scope. However, we can also have global variables. These are variables that are created outside of the function. Global functions are accessible to the function. Global declarations are practically the only statements that can be written outside of a sub or function.