Main Content

You are here:

Function Declarations in AS3

Hello and welcome. This tutorial is to explain in more detail the function declaration used in the Hello World Tutorial. Here is that function declaration:

public function sayHello(nameof:String):String{

A little about functions first. Functions are bits of reusable code. A user-defined function may receive a list of arguments (inputs), perform a set of commands (do stuff) and return a value (output something).

Back to that sayHello function. This is how it is laid out:

accessModifier static? function name(argument:input type):return type{

That looks complicated, but it can be broken down into parts which have to be in roughly the correct order.:

public means it can be accessed by the .fla. That word controls access, which we will look at some other time. Public and Private are the two commonest forms. You only need this when it is within a class - you can have a normal function, but when you deal with objects and classes it is neccessary.

If there were a static, it would mean that you didn't have to create an object from that class before you can use the function - you can use it straight off the template! It is optional.

function sayHello says that we are going to create a function called sayHello. You have to put "function", say hello is the name. This bit is required wherever the function is.

(nameof:String) says that this function requires an argument called nameof and that this argument has to be a string ( a string is a bit of text, like when we talked about variables). Arguments are bits of information a function needs, or they can be optional. In this case they are definitely necessary - we need the name of the person! Later on, we can give the function this bit of information, by putting it within the brackets when calling the function.

:String{ says that our argument is going to return a string - in other words, it is going to tell us some text when we want it to. There is a special term that can be used to say that the function returns nothing - void.