General Flex Tutorial – Part 3 – What is ActionScript (variables & functions)?

This is the third in a series of posts making up a general tutorial on Adobe Flex. In this post I begin describing ActionScript, the programming language used to code the business logic in Flex applications. In this post I cover variables and functions.

 

What is ActionScript? 

ActionScript is a scripting language based on the ECMAScript standard on which JavaScript is based. ActionScript is used to define the Model and Controller of your application. JavaScript is an interpreted language, meaning it is processed as it is executed, whereas ActionScript is a compiled language, meaning it is processed (compiled) as you are writing your application, which leads to faster execution.

 

Much of your ActionScript code will consist of variables, functions, and classes, in addition to a variety of other ActionScript constructs for program control, such as conditionals, looping, etc. This section on ActionScript will provide an overview of variables, functions, classes and some of the other major programming constructs.

 

Variables – A variable is a named entity used to store some data, such as a number (49.99), a string (”Welcome Greg”), a Boolean value (true or false), a date (04/14/2009), etc. Variables correspond to properties and style properties in MXML.

 

To use a variable, you need to declare it and assign it a value. You can declare the variable and assign an initial value later, or you can assign a value when the variable is declared. Here are some examples:

 

var validuser:Boolean;
var firstname:String = "Greg";
var myVbox:VBox = new VBox();
var currAdminPanel:AdminPanel = new AdminPanel();

 

Notice that a colon separates a variable from its “data type” (Boolean, String, AdminPanel).

 

Also notice how instances of classes (VBox and AdminPanel) are created above using the “new” keyword.

 

“Modifiers” can be added to variable declarations to affect aspects of the variables:

private var firstname:String = "Greg";
public static var num:int;
protected const MULTIPLIER:uint = 50;

 

Each of these examples includes keywords that result in subtle variations in the qualities of the variables:

private, public, protected, and internal specify what parts of your code can use the variable.

var indicates the variable can change, and const indicates it is a constant, and once set cannot change.

static specifies a variable that has one value for all instances of a class. Without the static keyword each class instance has its own instance of the variable that can have a different value.

 

KEY POINT: you can declare and initialize variables outside functions, but you can only use and manipulate variables within functions (discussed next). So you can declare and initialize a variable num in a single statement outside a function, but you cannot assign it a value outside a function. If you are trying to use a variable outside a function and your statement does not include the “var” keyword, there is a good chance your statement will fail.

 

var num = 50;          

okay – declaring and assigning a variable on the same line

 

var num;                   

okay – declaring a variable

num = 50;                 

error – cannot assign value to variable outside a function unless declaring it

 

ActionScript Primitive Data Types

Here are the ActionScript “primitive” data types. All other Flex data types are objects.

String: a textual value, like a name or the text of a book chapter

Boolean: a true-or-false value, such as whether a switch is on or whether two values are equal

Numeric: ActionScript 3.0 includes three specific data types for numeric data:

       Number: any numeric value, including values with or without a fraction

       int: an integer (a whole number without a fraction)

       uint: an “unsigned” integer, meaning a whole number that can’t be negative

 

Functions – A function is used to do something, such as add two numbers, examine a string and report on its value, calculate the time span between two dates, respond to user interaction with you application, etc.

 

You define a function by specifying its “signature”. Here is a typical ActionScript function signature:

private function addNumbers(num1:uint, num2:uint):uint {
       var result:uint = num1 + num2;
       return result;
}

 

Notice functions can be defined using “access modifiers” (private, public, protected) like variables to control what areas of your code can access the function.

 

The important parts and keywords of the function signature are:

function – this is what indicates this is a function, and not a variable (var) or a constant (const)

addNumbers – this is the name of the function

num1:uint – this is the first data parameter passed into the function for its use. num1 is the name of the parameter and uint is its data type.

uint after the closing parenthesis and colon is the data type of the value returned to the code that called the function. If the function does not return a value, this will be void. If a return value is specified, the function must return a value, and if the return data type is void, the function cannot return a value.

 

Statements that carry out the function’s work are enclosed in opening and closing curly braces {  }. Functions do not need to accept parameters between the parenthesis, and if they accept them, the function does not need to make use of them in the function body enclosed in the { }.

 

The variable “result” declared within the addNumbers function above has a “scope” that is local to the function, and cannot be referenced outside the function curly braces {  }. Keywords such as access modifiers private, public, protected and internal, and also other keywords such as static, etc. cannot be used inside functions.

 

The …rest parameter, allows you to pass any number or parameters, so in a function defined as myFunction(… myParams), you refer to the parameters in the function body as array elements, myParams[0], myParams[1], myParams[3], and you can pass in any number of parameters.

 

private function myFancyFunction(... myArgs):void {
       for (var i:uint = 0; i < myArgs.length; i++) {
              trace(myArgs[i]); // trace() prints data to the Flex Builder Console view
       }
}
myFancyFunction (1, "Greg", true);

 

Output

       1

       Greg

       true

 

As you can see, the parameters included in the … rest parameter can be of any type, they don’t all have to be uints, Booleans, Strings, etc. The … rest parameter can be used with other parameters, but it must be the last parameter listed.

 

private function myFancyFunction(num:uint, ... myArgs):void {
       trace(num);
       for (var i:uint = 0; i < myArgs.length; i++) {
              trace(myArgs[i]);
       }
}
myFancyFunction (400, 1, "Greg", true);

 

Output

       400

       1

       Greg

       true

 

All parameters passed into functions are passed in by “reference”, except for primitive values Boolean, int, Number, String, and uint. Passing by reference means you can pass as parameters data defined outside the function, and the data outside the function will be modified if the function changes the data. Primitives are passed by “value”, and data outside is not affected by modifications inside the function.

Tags: , ,

Leave a Reply

You must be logged in to post a comment.