23

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?

Rob Segal
  • 7,515
  • 12
  • 43
  • 72

3 Answers3

40

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
C. Ross
  • 31,137
  • 42
  • 147
  • 238
4

With classic ASP, you need to use 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
Oded
  • 489,969
  • 99
  • 883
  • 1,009
3

As has been pointed out you can use Exit Function but you should use caution. In the simple example you gave there really is no advantage no other code would have executed anyway.

Placing exit points throughout a chunk of code can make it hard to follow and debug. More seriously it can lead subsequent changes to the code being harder, requiring more extensive changes and therefore increasing the risk. Hence such a pattern should be considered a "bad smell".

A typical scenario where its reasonably acceptable is where the code may make some assertions on its input parameters before continuing with the body of the code. Other than that you should be able to express a really, really good reason to do it.

You may say "If I do it that way I'll have more If structures and increase the identation in the code excessively". If that is so then the function has too much code in it anyway and should be refactored into smaller functions.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • 1
    Some good points Anthony. It is definitely a very simple example and I used it just to find out what the proper syntax was. I was indeed going to mention your last point about having more "if" structures and increasing indentation if you don't use "exit". Personally I think this is a "religious war" issue in programming being more a matter of personal preference. Regardless your feedback is great so thank you. – Rob Segal Feb 04 '10 at 19:45
  • 1
    @Rob: I would define a "Religious" issue as a issue where choosing one side or another has little or no impact on the outcomes. Best practice on the other hand is about issues where choosing one side or another can significantly impact the final outcomes. As a general rule (bearing in mind no rules are ever absolute) it is "best practice" for any recognisable block of code to have one point of entry and one point of exit. This is true of functions, if statements, loops etc. `Exit Function` is a useful tool when a reasonable __exception__ to the "best practice" is found. – AnthonyWJones Feb 04 '10 at 22:41
  • 1
    True it certainly makes sense to have one point of entry and exit for functions and I would agree it is a good practice. My main reasoning for using "Exit Function" call would be for a point you have already mentioned which is performing various assertions and/or validations. – Rob Segal Feb 05 '10 at 15:56
  • I know I am 7 years late, but in other languages typing `return "What's up"` would exit the function anyway... I came here because I wanted to return false in a function but the rest of the code was executing and messing up the structure. I completely agree that exit points are bad, but in this situation they are normal code. Return exits the function and so `Exit function` is rightly called. `goto` on the other hand... – JustCarty May 16 '17 at 21:04
  • Also late, but there can be reasons to do it: say for example a function which returns true/false based on multiple conditions, if one of those conditions is expensive to evaluate it can be worth structuring the function to do the cheap test early and if false, return early, then only if the cheap test is true continue to the more expensive checks. – Adam Parkin Jan 10 '19 at 21:38