AS3 | Flash | Tips

Common Flash Compiler Errors: #1126

This error shows the following in the Compiler Errors window:


1126: Function does not have a body.

Quick Answer and Solution

By double-clicking on the error it will take you to the offending line of code. This error commonly happens when the end of line designator “;” is placed at the end of a function name declaration, as in:

1
2
3
4
function init() : void;
{
	/*function body*/
}

Notice the presence of the semicolon at the end of line 1. To fix the error the semicolon is removed:

1
2
3
4
function init() : void
{
	/*function body*/
}

Technical Overview

A function is a block of code that will only run when it is “called,” meaning the above example would be run by writing the function name elsewhere, like so: init();. When a particular function is called, the code that appears between the curly braces (“{}”) is run. This is referred to as the function “body.” When you test a movie in Flash the code is read line by line by the compiler and eventually turned into a SWF. However, what you see as a single line of code and what the compiler sees as a single line of code are two different things. The compiler ignores extra white space and carriage returns. For example, to the compiler the following function declaration:

function init() : void
{
	/*function body*/
}

…is the same as:

function init():void{/*function body*/}

Because of this, it depends on the semicolon and closing curly brace (“}”) to denote that a line of code has ended. The closing curly brace is used for every piece of code that creates a block of code (functions, for example), while the semicolon is used for everything else. If a semicolon were to appear before the opening curly brace (“{“) in the above line of code, the compiler would hit the semicolon and throw an error that the function did not have a “body” because it had reached the end of the line of code without encountering an opening and closing curly brace.

Note: The one exception to this rule is the constructor function found in class files, which can oddly enough contain a semicolon after the function name declaration and the compiler will not complain, for example the following will not throw an error:

package
{
	import flash.display.MovieClip;
 
	public class Main extends MovieClip
	{
		public function Main();
		{
			/*constructor function body*/
		}
	}
}

However, while the compiler will not complain, the constructor function actually ends at the semicolon, so what’s written between the curly braces (the “constructor function body”) will not be run when a new object of this class is instantiated (well, actually it will run, but it will be in the scope of the class, not the object, so it will run prior to the creation of the object). Constructor functions are different in one other aspect as well, they do not specify a return type (no “:void” after the parentheses, for example).