View Full Version : Need help with functions
Jim Carr
January 8th, 2009, 05:23 PM
I'm just getting into this and can't seem to make the simplest thing work! I'm trying to learn to define and call functions. Can someone please tell me what's wrong with the following? I get no errors but it won't trace the variable:
//define and fill a variable
var name = "jim"
//define a function
function traceTheName ():Void{
trace (name);
}
//call a function
TraceTheName();
GrndMasterFlash
January 8th, 2009, 05:38 PM
dude when you call a function you have to use the exact lettering....
traceTheName
is different than
TraceTheName
pay attention to your capitols and lower case
Jim Carr
January 8th, 2009, 05:44 PM
Well at least now I know how to call it. Thanks for straightening me out.
I'm just getting into this and can't seem to make the simplest thing work! I'm trying to learn to define and call functions. Can someone please tell me what's wrong with the following? I get no errors but it won't trace the variable:
//define and fill a variable
var name = "jim"
//define a function
function traceTheName ():Void{
trace (name);
}
//call a function
TraceTheName();
dustinsmith08
January 8th, 2009, 05:55 PM
there are a couple of problems, all of which are simple things everyone does when starting out.
first you used "name" as a variable. you may have noticed that the actionscript window made it blue. This means that "name" is a variable term that used by built in classes. Sometimes using it yourself causes a problem (like in this case) and sometimes it doesn't. In general I just don't use any term as a variable name that turns blue (aka any term by built in classes).
next - you mispled you function name when you called it. I have done this a lot my self, you defined the function as "traceTheName" - when you called it you wrote "TraceTheName" - notice that you capitalized the "T" when you called it. As far as actionscript is concered you could have wrote "sdfsfdssdfs" because variables are caps sensitive.
last you capitalized the "V" in void after the function. you need to use "void" with a lower case "v" - or you could just leave it out and that would work also.
here is the corrected code:
//define and fill a variable
var someOtherName = "jim"
//define a function
function traceTheName ():void{ // if you erase "void" everything will still work
trace (someOtherName);
}
//call a function
traceTheName();
good luck
scottc
January 8th, 2009, 05:55 PM
some more tips...
- actionscript is entirely case sensitive, not just function names.
- change :Void to :void
- add a data type to all variables, if you cannot decide you can use wildcard :*
//from this:
var name = "jim"
//to this:
var name:String = "jim";
//a number example
var age:Number = 10;
TheCanadian
January 8th, 2009, 09:06 PM
first you used "name" as a variable. you may have noticed that the actionscript window made it blue. This means that "name" is a variable term that used by built in classes. Sometimes using it yourself causes a problem (like in this case) and sometimes it doesn't. In general I just don't use any term as a variable name that turns blue (aka any term by built in classes).
Although you're right that he shouldn't use name (cause it messes with objects in the display list), you don't have to always avoid indentifiers because they turn blue.
http://www.kirupa.com/forum/showthread.php?t=249918
Krilnon
January 8th, 2009, 09:30 PM
Fun fact: some future reserved words sometimes cause random, silent compilation errors/issues. It's nothing serious, but I once used intrinsic (or something) as a namespace name (knowing that it was a future reserved word, but I had a good reason to use that particular name) and it took me a little while to realize that the name was the problem (since what I was doing was ridiculous anyway).
For example, writing virtual::name (or something similar as long as name exists in an open namespace) when there is no namespace named virtual causes the program to run as though you were accessing the variable from the open namespace, not virtual.
TheCanadian
January 9th, 2009, 01:08 AM
Putting intrinsic back in kind of seems like a step backwards. Where did you find this info?
Krilnon
January 9th, 2009, 03:45 AM
I don't think that something being a future reserved word means that they're going to put it back in necessarily, just that it might be later, or something silly like that.
Well, if you want the whole story:
Once upon a time, I was writing some code and I somehow came across AS3's global object. I think the code I had at the time looks like this, at least, this is how I always get the global object now:
var global:Object = function(){ return function(){ return this }() }();
trace(global); // [object global]
But, you know how you always hear how there is no global object in AS3. I wanted to find out more about it, especially to find out if there was anything cool that could be done with it, so I looked it up in the ES4 draft specification. I found this one passage in it that proposed a short way to always be able to access that object:'
PROPOSAL — The expression
intrinsic::global
evaluates to a reference to the global object in effect for the calling code. Please see the globals proposals page for more information.
So, I then tried that shorthand in my code, and it worked. The thing that it took me a while to realize was that that only worked because I just happened to have a variable named global already in the default namespace.
Eventually, I reduced it down to a bug/issue with the compiler and a few keywords and future reserved words (virtual, native, and intrinsic, I think). I reported it, but because I initially reported it in a way that made it sound like it needed to cause a compiler error, it was moved to the Flex bugbase instead of the ASC one. I thought that that decision was a bit silly because you get a silent failure with weird behavior using the IDE compiler; there's no reason that using a future reserved word should result in different behavior than a regular identifier name, like intrinsic2, or something. Anyway, that's the story that I'm sure some people are having difficulty tying back to the original topic by now, but whatever. :) Also here's my 'deferred' bug entry: https://bugs.adobe.com/jira/browse/SDK-18148
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.