It's hard to tell what code you want to move to a Function. A couple of things to consider:
If you pass objects to a method BYVAL, you are passing a COPY of the object (i.e. your integer). If you pass an object to a method BYREF you are NOT passing a value, you are passing a pointer to the original variable (and can generally update it from your subroutine). Here's an example:
' This is a simple, byval function. We'll call it in the mySub method.
private function AddOne(BYVAL AnInteger as INTEGER) as INTEGER
dim intPlusOne as INTEGER
intPlusOne = AnInteger + 1
return intPlusOne
end function
' This is a BYREF function.
private SUB AddOneByRef(BYREF AnInteger as INTEGER)
AnInteger = AnInteger + 1
end sub
' Now we call the methods above.
private sub mySub()
' intX starts out as a 5
dim intX as INTEGER = 5
' intX = 5
intX is passed ByVal to AddOne. intX does not change.
dim intY as INTEGER = AddOne(intX)
' intX = 5, intY=6
AddOneByRef(intX)
' intX=5 before the call, intX=6 after the call. No RETURN is needed.
' We updated the variable 'intX' within the subroutine.
end sub
To start, figure out what you want to make repeatable (that's the whole point of creating a method like this). Once you establish that, look at the code to be split out and determine every piece of data that it needs - every variable, every fixed value. Look at the scope of your variables - does the entire Class have access to them, or were they all declared local to the subroutine snippet you provided?
To successfully convert procedural code to a function or sub, you have to know what you're going to pass and how. Creating class-scoped variables (meaning that you declare them outside of any subroutines, such as at the top of your class immediately after the class and inherits declarations) means that you can use them from within any method without concern for passing them. Don't arbitrarily scope high, though. You, as the developer, have to know where to scope your variables to get maximum efficiency and minimal memory requirements. Remember that if you scope at the Class level, that variable will not be subject to Garbage Collection until that class is unloaded.
The code you gave as an example is pretty short, and I would not glance twice at it as far as trying to split out functions and subroutines (it's not that complex). However, it appears that you're trying to get your feet wet with understanding Functions and Subs, so you've picked a good starting point.
See what you can do with what I've provided, and if you want more specific help, identify the lines that you see as a candidate for a function or sub.
Happy Coding!
Ned