Gamgam0
98 posts
|
Hey i was wondering if you could store a parameter of a function as a variable. For example like if you had an achievement could you have code along the lines of:
getAward(5);
// I want this to trace “You got award number 5”
function getAward(){
var awardnumber = im not sure exactly what to put here or like parameter value
if(awardnumber = 4){
trace(“You got award number 4”);
}
if(awardnumber = 5){
trace(“You got award number 5”);
}
}
I know this code wont work but this is kind of what i want to do (but i dont want it for an achievement system)
What I am really asking is can you save a parameter of a function as a variable.
Please tell me if you need clarification. Thanks in advance.
|
|
|
Moonkey
1007 posts
|
You declare parameters in the function header. Then they are automatically given the value that you pass in when you call the function, and you can use them the same as you use local variables.
function getAward(awardnumber:Number) {
|
|
|
faiuwle
116 posts
|
Of course, but you don’t have to actually explicitly do so, because if your write your function header right, that’s exactly what you’ve already done. If the function header doesn’t list any arguments, you can’t pass any when you call it anyway.
It should be:
function getAward (awardNumber:int):void {
// whatever
}
Also, = and == are different. If you don’t understand how, you’re in for a lot of frustration.
|
|
|
faiuwle
116 posts
|
(What’s the difference between int and Number, exactly?)
|
|
|
SuperFalconKick
98 posts
|
Number is floating point.
|
|
|
faiuwle
116 posts
|
There is no float or double then, I take it? Is there a compelling reason for using a floating point number when you just want an integer, especially due to imprecisions in floating point math?
|
|
|
zero579911
411 posts
|
|
|
|
Moonkey
1007 posts
|
Is there a compelling reason for using a floating point number when you just want an integer
No. For some reason I was thinking as2 (where there is no int type)
|
|
|
SuperFalconKick
98 posts
|
void means the function doesn’t return anything. There’s no float or double, and there’s no reason to use Number for and interger. people use it because they don’t know any better, similar to how they always use MovieClip when Sprite or Shape would do.
|
|
|
zero579911
411 posts
|
Oh so people use void because they don’t know anything else better to use?
So Void is used to check if it doesn’t return anything?
|
|
|
UnknownGuardian
6214 posts
|
How about condesning it and writing:
public function getAward(awardNumber:int):void
{
trace("You got award number " + awardNumber);
}
Or if you are not comfrotable in strong/strict typing:
function getAward(awardNumber)
{
trace("You got award number " + awardNumber);
}
Calling getAward(5) traces You got award number 5.
Calling getAward(1000) traces You got award number 1000.
|
|
|
Jabor
11382 posts
|
Void indicates that the function will not return anything. At all.
It means that the compiler will throw up an error if you try and use the return value, instead of you getting a mysterious error at runtime and having to debug it.
|
|
|
GhostIV
16 posts
|
Originally posted by zero579911:
Oh so people use void because they don’t know anything else better to use?
So Void is used to check if it doesn’t return anything?
Ummm… no. Void means function doesent have any return value.
Normally, in c++ for example, every function has some return value. So people write their function headers saying what they want to return. For example:
public int myFunction(double param1){
...
In the end of the function body we have to write something like “return n” and it would return the n, which was defined as integer in our function. It means that we return an int value and we may write something like this when we call this function:
i = myFunction(n);
And “i” would have some number.
But we may not want to return anything. The problem is that even if you dont define your return type the compiler thinks you return int by default. And if he doesent see the “return” he throws a compile error. To tell the compiler you dont want to return anything you write “void” instead of the return type, saying that this function doesent have anything to return and thus, has no “return” operator.
Its not necessary in AS3, as its a lot less strict than c++, c# and the like. But its considered a good style to put void in return type still.
|
|
|
faiuwle
116 posts
|
For the record here, I am working with AS3 in FlashDevelop, and there is a compiler error if I declare a non-constructor function without a return type. AIUI, AS2 is much more lenient about that sort of thing.
zero, what they were talking about about people not knowing any better was using Number instead of int, nothing to do with void. Think about it this way: when you declare a variable, you generally say
name:type
e.g.
var myNumber:int
indicating that myNumber is an int(eger) and can only be used as such elsewhere in the program. Similarly, functions can be treated as ints too, for example:
function timesTwo (…):int
You might want to say something like
var double:int = timesTwo (myNumber)
which would set double equal to two times myNumber. Since myNumber x 2 is also a number, it makes sense to treat it as one and put :int after the function header. (If the examples in earlier posts are confusing, it’s probably because in C++ the type comes first. Don’t worry about that.) Adding :void instead of :int (or something else) tells the program that that function can’t be treated as any kind of value.
|
|
|
zero579911
411 posts
|
|
|
|
UnknownGuardian
6214 posts
|
In relation to the void questions, can’t you still have the word return in void methods, but just to kill(stop) them. like this:
function getMeatbagsWhoHateJabor(arr) //arr is an array of meatbags
{
for(int i = 0; i < arr.length;i++)
{
if(arr[i] == "Moonkey")
return; //stops method?
}
}
|
|
|
UnknownGuardian
6214 posts
|
woops. Forgot to close my loop. Fixed.
|
|
|
Gamgam0
98 posts
|
What is the difference between = and ==
|
|
|
SuperFalconKick
98 posts
|
Ones an assignment operator the others a test for equality.
|
|
|
ssjskipp
258 posts
|
Originally posted by Gamgam0:
What is the difference between = and ==
A = B; //Stores the value of B into A
A == B; //Test to see if A is equal to B
A === B; //Tests strict equality (class type AND data) between A and B
|
|
|
GhostIV
16 posts
|
A === B; //Tests strict equality (class type AND data) between A and B
Wow. Didnt know that. Thank you:)
Originally posted by UnknownGuardian:
In relation to the void questions, can’t you still have the word return in void methods, but just to kill(stop) them. like this:
No idea. Maybe you should chack it out.
|
|
|
faiuwle
116 posts
|
Yes, void functions can have “return” in them to kill them. I’ve heard it’s “bad” to have functions do things like return in the middle of loops and such, but I do it all the time, e.g.
if (param <= 0) return;
or
for (var x:int = 0; x < param.length; x++)
if (param[x] <= 0) return;
to cause the function to return immediately if the parameters are invalid or otherwise nonsensical, to cut down on potential bugs.
|
|
|
Jabor
11382 posts
|
I’ve heard it’s “bad” to have functions do things like return in the middle of loops and such
It’s not going to cause any memory corruption or anything.
It’s more because it makes the code harder to follow if there are exit points in the middle of loops that actually do stuff – it means that if you’re scanning the code, you can’t just note what the loop does and then skip past it, but instead have to work through it and figure out under what circumstances it’s going to mean leaving the function.
For simple parameter validation it’s fine, but if you’re doing more complex stuff in the loop as well it’s probably not a good idea.
|
|
|
faiuwle
116 posts
|
Yeah, by “bad” I meant that people simply consider it bad style.
|