Passing a variable from JS to Applescript

Why is this not working?

on getMean(theFile)
	tell application "Adobe Photoshop CS4"
		activate
		try
			open theFile
		on error errmsg
			error "Open Error: " & errmsg
		end try
		set docRef to current document
		flatten docRef
		if bits per channel of docRef is not eight then set bits per channel of docRef to eight
		if mode of docRef is not grayscale then change mode of docRef to grayscale
		try
			set theMean to do javascript "
				var re = RegExp( '[123456789]' );
				app.activeDocument.activeLayer.applyAverage();
				var arithMean = re.exec(app.activeDocument.channels[0].histogram.toString()).index/2;
				return arithMean;
				" -- document must be flat, 8-bit, and grayscale
		on error
			set theMean to false
		end try
		close docRef saving no
	end tell
	return theMean
end getMean

I get this error: “illegal return outside of function body”.

I used this article. It looks like I am doing it right, but I might be missing something:
http://hints.macworld.com/article.php?story=20051217102738896

In JavaScript you can ONLY return from within the body of a defined function. Or so I think.

on getMean(theFile)
	tell application "Adobe Photoshop CS2"
		activate
		try
			open theFile
		on error errmsg
			error "Open Error: " & errmsg
		end try
		set docRef to current document
		flatten docRef
		if bits per channel of docRef is not eight then set bits per channel of docRef to eight
		if mode of docRef is not grayscale then change mode of docRef to grayscale
		try
			set theMean to do javascript "
               function getMean() {
		var re = RegExp( '[123456789]' );
               app.activeDocument.activeLayer.applyAverage();
               var arithMean = re.exec(app.activeDocument.channels[0].histogram.toString()).index/2;
               return arithMean;} getMean();
               " -- document must be flat, 8-bit, and grayscale
		on error
			set theMean to false
		end try
		close docRef saving no
	end tell
	return theMean
end getMean

The function.

function doSomeThing() {
// Your code goes here
}

Then call it.

doSomeThing();

All within your string.

Hey, there we go. I tried putting it all inside of function brackets, but I didn’t name the function or call it. Thanks!