dcsimg
First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Steve Happ | Website: http://www.video-animation.com

Functions

A function is a named unit, in which statements are logically grouped. It usually contains the following parts:

  • Function Name - e.g. function someName( )
  • Argument list - is enclosed within parentheses after the function name. It contains a comma-separated list of 0 or more arguments. Arguments are passed to the variable with the same name as the argument inside the function.
  • Return Type - the datatype of what the function can evaluate to.
  • The function Body - is enclosed within a pair of braces { } and contains a sequence of statements.
Here is a template. For AS 1.0 :
Function functionName(arg1, arg2, arg3){
	// statements go here
	return something if not void return type;
	}
example1 (AS 1.0). Type this and compile (Control+Enter):
function test1(arg1, arg2){
// arg1 is passed to the variable with the same name 
// in the next line I.e. arg1
	trace(arg1);
	trace(arg2);
}
// call function
test1(3,"Steve");
Notice that to call a function we write the function name and the argument lists in parentheses. Template to call a function:
  functionName(arg1, arg2);
Example 2 with a return value (AS 1.0)
function returnTest(arg1){
	var num = arg1 * 10;
	return num;
}
// call function
trace(returnTest(5));
ex. 3
// returns the absolute value of number
 function abs(number){
return( i < 0 ? -i : i);
}
// call the method
trace(abs(-350.95);

ex 4
// return the smaller of two values.
function min(num1,num2){
return(num1 <= num2 ? num1: num2);
}
trace(min(27,45));

Functions in AS 2.0

Functions in AS 2.0 are a little different. AS 2.0 allows the setting of specific datatypes. Here is an example :

Function avgReturn(arg1:Number):Number{
Var num = arg1 * 5;
return num;
}
The template for an AS 2.0 function could be :
Function functionName(arg1:DataType, 
		arg2:DataType):ReturnType{
// statements go here
return something ;
}
Void is used to specify that there is no return type. E.g.
function noReturn(arg1:Number, 
				arg2:String):Void{
trace(arg2+" is a string and "+arg1+" is a number");
}
// call function 
noReturn(5,"Steve");

Arrays

An array is a collection of objects . Individual objects are not named but accessed by its position in the array. Try this!

var theArray = new Array(5,4,8,2,1);
for(var i = 0;i<5;i++){
	trace(theArray[i]);
}
Elements are numbered beginning with "0".
In the above array
theArray[0] = 5
theArray[2] = 8
Length is a property of Array. It tells how many elements are in the array. So , from above,
theArray.length = 5

Classes

(As 2.0 only)
A class is a user-defined datatype, an aggregate of named data elements, and a set of operations to manipulate that data. A class definition consists of two parts:

  1. The class head - composed of the keyword class and a name
  2. the class body - enclosed by braces, contains the member definitions
A class name represents a new data type . e.g.
class Dog{
	// data members
	var name:String;
	var age:Number;
	// class method
	function setAge(age1:Number){
	age = age1;
	}
}
Constructors allow us to create an instance of a class . e.g.
var rusty:Dog = new Dog();
// the dot operator (".") 
// allows us to access members and methods of that class.
rusty.setAge(7);
rusty.name = "Rusty";

For more information and tutes , go to my site.

» Level Basic

Added: 2005-08-10
Rating: 8 Votes: 12
(10 being the highest)
» Author
Steve Happ is the founder of Steve's Tutes and has been going hard at it developing Flash for Web and CD-rom.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.