Functions, getting data in and out.

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

I have a script with several functions. I think I understand how to feed something into a function variable, example:

function myfunction(var1, var2){
blah blah;
return myresult1, myreult2;
}

myfunction(data1, data2);

How many variables can a function return? Some of my functions need to return multiple variables to use in other functions. What's the best way to do that? I'm having trouble getting one function to output something to another function.
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

Never mind, I think I have it working now. I put my variables in an array and then just pulled what I needed out of the array when sending variables to the next function. Is that the best way to do this?
davestewart
Posts: 114
Joined: March 10th, 2005, 5:50 am
Location: London, UK
Contact:

Hi Byron,

An array is good way to send through complex data. Another option would be to use an Object. You can create one using the Object object:

Code: Select all

data=Object()
data.name="Ben"
data.data="On wednesdays I go shopping"
or shorthand:

Code: Select all

var data={name:"John",data:"Once upon a time"}
or you could create a class constructor, but thats probably too much info for now.


One of the advantage of using an object over an array is that its easier to remember properties of the object, rather than indices of an array.

Try this to test:

Code: Select all

function editData(obj){
	obj.data=5
	obj.name="Dave"
	return obj
	}

	
var data={name:"John",data:"Once upon a time"}
alert([obj.name,obj.data])

data=editData(data)
alert([data.name,data.data])

Cheers,
Dave
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

That's good to know, thanks!
davestewart
Posts: 114
Joined: March 10th, 2005, 5:50 am
Location: London, UK
Contact:

Glad to be of help!
Out of interest - what sort of 3d do you do? Which package are you using?
TTFN, Dave
Post Reply