Returning early from a function in classic ASP

asp-classic

Is there a way to return early from a function in classic ASP rather than it run the full length of the function? For example lets say I have the function…

Function MyFunc(str)
  if (str = "ReturnNow!") then
    Response.Write("What up!")       
  else
    Response.Write("Made it to the end")     
  end if
End Function

Can I write it like so…

Function MyFunc(str)
  if (str = "ReturnNow!") then
    Response.Write("What up!")       
    return
  end if

  Response.Write("Made it to the end")     
End Function

Note the return statement which of course I can't do in classic ASP. Is there a way to break code execution where that return statement sits?

Best Answer

Yes using exit function.

Function MyFunc(str)
  if str = "ReturnNow!" then
    Response.Write("What up!")       
    Exit Function
  end if

  Response.Write("Made it to the end")     
End Function

I commonly use this when returning a value from a function.

Function usefulFunc(str)
   ''# Validate Input
   If str = "" Then
      usefulFunc = ""
      Exit Function
   End If 

   ''# Real function 
   ''# ...
End Function