The Linux Page

RETURN not working in Visual Basic & Arrays...

RETURN statement in BASIC

Like many programmers, I switch between languages quite often.

I have worked with BASIC before since I had an Apple //c and used other Apple ][ computers that had some (really bad, early) BASIC language in their ROM.

At the time, the worst part was the line numbers. You had to write in BASIC as if you were writing assembly language. And I'll skip the numerous problems with memory...

Today, that's much better, but the language is still built on what was available then. Thus, you still have the GOSUB instruction available to you. That is used to change the current execution pointer to a label, like the GOTO instruction. The main difference is that the GOSUB pushes the pointer of the next statement on the stack and thus it is possible to come back right after the GOSUB once you are done. To come back, you use the RETURN instruction.

The fact is that all the other languages use the RETURN instruction in functions to return the result. That's true in Perl, PHP, C/C++, Ada, Pascal, Lisp, Logo, FORTRAN, and even Bourne shells. So for me to switch back to the use of RETURN in BASIC, I have to say, is a bit tricky.

And then, you may ask... so... how do you return the result of a function? This is the same as in Ada and Pascal. You set the special variable named as the function name to the value of the result. So for instance, if you have a function testing whether a value is empty:

	Function IsDefined(value) As Boolean
' By default, return false IsDefined = False If IsEmpty(value) Or IsNull(value) Or value = "" Or value = 0 Then
Exit Function
End If
' It worked, return true IsDefined = True End Function

As you can see, to exit the function, you say Exit Function and not Return. And the variable IsDefined is the name of the function and that's what includes the result of the function. The type specification is optional and is defined as in Ada.

Returning an array

As you would expect, once in a while you may want to return an array. This is possible. First declare your function to return as <type> followed by parenthesis (like an empty array declaration).

Now, it won't be possible to use the name of the function followed by parenthesis to setup a value in your array. This is because in that case the interpreter wants to generate a function call!

In other words, it is not possible to directly set the items of the array in the output array.

The solution is pretty simple, you create a temporary array inside the function and before returning, copy that array in the result (i.e. function name = temporary array).

For example:

	Function MultiResult(value) As String()
		' Temporary variable inside our function
		Dim Out(3) As String
		If value = 5 Then
			Out(0) = "T1"
			Out(1) = "T2"
			Out(2) = "T3"
			Out(3) = "T4"
		Else
			Out(0) = "E1"
			Out(1) = "E2"
			Out(2) = "E3"
			Out(3) = "E4"
		End If
		' Now we can set the result
		MultiResult = Out
	End Function
Syndicate content

SMS From Me Logo

SMS From Me

To automatically start one on one conversations with your online leads.

     

   

Terms of Site Index

Find the page/content you are looking for with our index.