Page 1 of 1

AS 2.0 Basics 4 - Functions [Flash 8] Rate Topic: -----

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Post icon  Posted 28 July 2008 - 04:04 PM

Functions make doing things multiple times with different values very fast and easy, however if you haven't used functions in the past they can be quite a hassle to get into and understand.

Hello Function new to the world

We will start by creating a simple function that will trace (output) Hello Function!. This is accomplished by creating a function which you define like so function FUNCTION_NAME(). For our current purposes we will name the functio hello_func like so: function hello_func(). The () is where you typically place any parameters (variables) that you are passing into the function for its use, however for the moment we will be passing in no parameters so it is left blank. Now we just need to tell the code where the function starts and ends and what to do. This is accomplished with the following code.

function hello_func(){
  trace("Hello Function!");
}


NOTE - This is all the code needed to declare our function!

Now we have a working function, but how do we get it to run?
This is a fairly simple answer, you call the function. This can be done by typing the function name and then the () with any parameters needed (in this case it is none). hello_func();

OUTPUT - Hello Function!.

Returning a value
Functions are also usefule because they can return a value. Say you wanted to know what 2+2 was; a function could tell you. You declare the function in the same way as above, but this time we need to change the contents of the function. We no longer need the trace("Hello Function!"); so we can delete that.

NOTE - For our purposes we will call this function adding (because add is a keyword in AS 2.0).

Then we place a return statement that returns the sum of 2+2 (4).

function adding(){
  return 2+2;
}
trace(adding());



After we have finished the function } we will have the script trace the value returned.

OUTPUT - 4.

Passing Parameters
Functions also allow you to pass parameters into them. This can be quite useful (taking adding as an example) if you want to know the sum of 2 numbers that aren't going to be available hard-coded into the function.

You need to start off by telling the function what to expect, this is accomplished as such: function FUNCTION_NAME(PARAM_1, PARAM_2)

We will now begin rewriting our adding function to add different numbers and output the sum. To start out we need to have the function looking for things to add together. Our new function will be coded as so:

function adding(a:Number, b:Number){
  return a+b;
}



This takes 2 numbers (say 1 and 3) and adds them together then returns the value (1+3 = 4). Passing parameters into i is simple too: adding(1, 3);.

Our new code looks like this:

function adding(a:Number, b:Number){
  return a+b;
}
trace(adding(1, 3));



OUTPUT - 4.

Beginning Defensive Programming so you don't error out all the time
Defensive programming (in this case) is making sure that you don't allow undefined variables to be sent into the function. If we were to call adding like so: trace(adding(1, )); the output looks like so:

Flash 8 Output window said:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Parameter name expected
function adding(var a:Number, var b:Number){

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Unexpected ')' encountered
trace(adding(1, ));

Total ActionScript Errors: 2 Reported Errors: 2


This isn't what you want to have happen if you are going to be putting this out on the web for people to use. So, how do you cover the bases?

It is a little easier than you would think, however Flash 8 (AS 2.0) requires some differing methods than normally used in programming languages. You have to use if statements (sadly, AS 2.0 doesn't allow you to set initial values for parameters being sent to functions).

For our case we will be checking if the numbers are set or not. If they aren't we will set them to 0, otherwise we will leave them as they are.

if(!a){
  a = 0;
}
if(!b){
  b = 0;
}


Now we just place this in the top of our function and we have some basic defensive programming going on.

function adding(a:Number, b:Number){
	if(!a){
		a = 0;
	}
	if(!b){
		b = 0;
	}
	  return a+b;
}


If we were to call adding with only 1 variable (or none) it would now return an answer and not error out:
trace(adding()); OUTPUT - 0.
trace(adding(1)); OUTPUT - 1.
trace(adding(0, 2)); OUTPUT - 2;

REALIZE - This is not perfect, if the user inputs something like ( , 2) it will still error out.

Returning Multiple Values because one isn't good enough
If you have multiple variables that are found in a function and want them all to be returned you may think that comma (,) delimiting them will do the trick (as it does in some languages). However, Flash and AS 2.0 are not other languages and will not allow this. If you attempt to do it you will get the last value in the line of values to be returned and nothing else.

There is a way to get around this though and it can be quite useful; you must use arrays. In this example we will return a, b, and the sum of the 2.

function adding(a:Number, b:Number){
	if(!a){
		a = 0;
	}
	if(!b){
		b = 0;
	}
	  return [a, b, a+b];
}


Now if we were to just trace out the returned values it wouldn't look right:
trace(adding(1, 2)); OUTPUT - 1,2,3.

So you will need to split the array apart to be able to use various portions of it. This is done like so:

var returned:Array = adding(1, 2);
trace("The value "+returned[0]+" plus "+returned[1]+" equals "+returned[2]);


OUTPUT - The value 1 plus 2 equals 3.

The thing to remember when using arrays in AS 2.0 is that you can't name items in them, you just have to use the number index that thye have, so pay attention to the order you place values into an array.

NOTE - Arrays always start with index of 0.

Returning Multiple Values using objects instead of arrays
I understand that everyone wants to be able to name their values for easy access, I do too. Luckily, with the help of objects you can! Just create a new object and set its values equal to whatever you want (and use whatever name you want too) then return the object and you will be able to call its separate values without needing to memorize the index of each. It can look like so:

function adding(a:Number, b:Number){
	if(!a){
		a = 0;
	}
	if(!b){
		b = 0;
	}
	var obj:Object = new Object();
	obj.a = a;
	obj.b = b;
	obj.sum = a+b;
	  return obj;
}
var obj:Object = adding(1, 2);
trace("The value "+obj.a+" plus "+obj.b+" equals "+obj.sum);

OUTPUT - The value 1 plus 2 equals 3.

NOTICE - I am not using indexes as in the array example above, but am using the varaibels names that I assigned to obj in the function. You can add and rename variables to be returned easily and quickly!

The End I hope that helps everyone understand how to use functions a bit more
See you next time :)

This post has been edited by BetaWar: 28 July 2008 - 07:14 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1