Why does my function not return my values?

Hello everyone,

I am struggling with a silly problem. Sometimes my functions just seem to refuse to hand over variables.For example this code does not work. b stays empty… Am I overlooking something simple?

Sub asdf
b = test()
End Sub

Sub test
test = array(1,2,3)
End Sub

Oh, not Sub - for a function that should return a value as a result of the string

test = array(1,2,3)

the correct keyword is Function

1 Like

Sub asdf
b = test()
End Sub

Sub test
test = array(1,2,3)
End Sub

Try changing “Sub test” to “Function test()”

darn, -.- thanks a bunch.

I have just tried doing it with a function, now the code jumps into the function but as soon as it reaches

“nameofcunc = output var” line it restarts the function from the beginning?

I wrapped an error handler around your code and added Option Explicit at the top. Those two things will help you troubleshoot in the future.

REM  *****  BASIC  *****
Option Explicit

Sub Main
On Error Goto ErrHandler
Dim x(3) As Integer
Dim i As Integer

	x = testFunction()
	
	For i = 0 To 2
			MsgBox "value at row " & i & " is: " & x(i), MB_OK, "Results of Loop"
	Next i

ExitSub:
	Exit Sub
	
ErrHandler:
	If MsgBox ("Error " & Err & " at line " & Erl & " - " & Error$ & Chr$(13) & "Continue?", MB_ICONSTOP + MB_YESNO , "Error in Sub Main") = IDYES Then
		Resume Next
	Else
		Resume ExitSub
	End IF

End Sub

Function testFunction() As Array
On Error Goto ErrHandler

	testFunction = array(1,2,3)
	
ExitSub:
	Exit Function
	
ErrHandler:
	If MsgBox ("Error " & Err & " at line " & Erl & " - " & Error$ & Chr$(13) & "Continue?", MB_ICONSTOP + MB_YESNO , "Error in testFunction") = IDYES Then
		Resume Next
	Else
		Resume ExitSub
	End IF

End Function