Technical Luddite

24 notes &

Classic ASP function syntax

Google-fu results: a number of simple examples, none of which thoroughly explained how to make functions that actually return variables.

The reason for this, I suppose, is because they work the same as functions in VBScript. Near as I can tell, putting <%@ Language=VBscript %> at the top of the page is unnecessary, as the syntax between the two is identical (or is implicit?). Found the <%@ Language=VBscript %> tag at the top of my header file, turns out I was actually having a problem with including it more than once (and not at the very top of the file).

The main thing throwing me was the way to return a value from the function - rather than using a return statement (like C/C++, Java, Ruby and Python), or even an implicit return value (the last value calculated - supported in Ruby and Python), ASP returns the value in a variable of the same name as the function (like Fortran, of all things). So, to return a variable from function foo, you’d write:

function foo(with, some, arguments)
  foo = "bar"
end function

And when you write the statement x = foo(with, some, arguments), the variable x would now contain the string “bar”. Note that if you return an object from a function, you will need to use the set x = foo(with, some, arguments) instead, to avoid calling the object’s default function.

There are also subroutines in ASP (again, like Fortran) which work differently from functions. You can’t return a value from a subroutine, and presumably that reduces overhead over not using the return value from a function. Personally, I doubt it’s particularly significant nowadays, and it can make things difficult because you need to call functions and subroutines using a different syntax. When you call a subroutine you must either include call at the beginning or not have brackets around the arguments. So, given:

sub foo(with, some, arguments)
  response.write("which does some things")
end sub
call foo(1,2,3) 'is valid, and
foo 1,2,3 'is also valid, but
foo(1,2,3) 'will return an error

Curiously enough, the interpreter will let you leave brackets around a subroutine as long as you aren’t passing any arguments to it.

Filed under asp classic asp programming vbscript functions syntax

  1. technicalluddite posted this