-
How do functions work?
I wonder how do functions work with parameters by REFERENCE.
Let's say in C++ you will write
function (&nameOfVar) the name of the var takes a "&" if you want to pass it by reference, but what is it in Flash?
Because I want that the vars I use in the functions to get changed in the end...
-
OOP is one letter from OOPS
If I understand you correctly, you want to pass a variable to a function, modify the value, and then return it?
If so:
//define the function
function changeValue(x){
x = someNewValue;
return x;
}
then when you call the function as such:
//with a number
var changedX = changeValue(5)
//with a string
var changedX = changeValue("bob")
will set the new variable var changedX to whatever the function does to the variable because you told the function to return a value at the end.
-
flash uses loose-typed variables.
this following won't output errors as would happen in a strict-typed environment:
Code:
var A=10;
if(A > 10)A=getTimer();
function bob(A){
if(!A) var A = Math.max(this.A,_global.A)||arguments.callee.A;}
-
I'm sorry, perhaps I wasn't clear enough... I mean passing by reference for functions that could have more than 1 vars.
e.g:
func = function(a, b, c) {
a=10
b=a+5
c=0 }
h = i = j = 0
func(h, i, j)
let's say you would like to change those 3 values, you can't use return with 3 variables...
In C++, that would have been: function(&a, &b, &c) ... To tell that those passed values will get changed...
Someone have a clue?
-
OOP is one letter from OOPS
Well on way you coulod do this if these variables have to be updated regularly is to define the globally or on the root. If they are on the root, they can be accessed both within and outside of the funcntion. Hence when the fuinction updates them they will eb avaiable anywhere else in the movie you would like to use them. So:
try this so you can see what I mean (i will build on your example). Creat a blank movie and put this code on the first frame of a layer then run it and take a look at the output panel:
var a:Number = 0
var b:Number = 0
var c:Number = 0
trace("a = " + a + " b = " + b + " c = " + c)
function changeValues(a, b, c) {
a=10
b=a+5
c=0
}
trace("a = " + a + " b = " + b + " c = " + c)
perhaps some other have a more efficient way of doing this.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|