Accessibility

ActionScript cookbook beta

Create a typed function

Problem Summary

You need to create a function that specifies the type of its arguments, as well as its return type.

Solution Summary

Use the function keyword and type identifiers to create a function that takes and returns a typed value.

Explanation

ActionScript 2

function foo(bar:String):Void
{
   trace(bar);
}

foo("hello");

ActionScript 3

function foo(bar:String):void
{
   trace(bar);
}

foo("hello");

Note: We specify the return type as void, which means that the function does not return a value. Also note that in ActionScript 3 the usage is void, and not Void as in ActionScript 2.

If you try to call the function in ActionScript 3 and pass it a Number instead of a String, you will get an error.