By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

Passing to/from function problems

P: n/a

Folks,
I have a simple script below... I come from a programming background
with PHP,C++,bash unix shell scripting so I have a rough understanding
when it comes to javascript. I have written a few javascripts of my own
but only until now have I realised that this will be the first time that
I have wanted to pass data *to* a function, chew it up, and spit it
back... In my test script below, I would have expected the script to
return def=1 and ghi=2, but it doesn't...

Can someone tell me where I've gone wrong? Thanks, all replies, via the
newsgroup would be much appreciated,

randelld
<script language="JavaScript" type="text/javascript">
function firstFunction(def, ghi)
{
def=1;
ghi=2;
return(def, ghi);
}

// create our variables
var def;
var ghi;

// set def=10 and ghi=20
def=10;
ghi=20;

// the function should now change def=1 and ghi=2
firstFunction(def,ghi);

document.write("def=" + def + " ghi="+ghi);
</script>
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
1) il faut retourner un tableau
2) appel se fait par valretour=mafonction (.....)
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
// create our variables
var def;
var ghi;

function firstFunction(def, ghi)
{
def=1;
ghi=2;
return arguments;
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<script language="JavaScript" type="text/javascript">

// set def=10 and ghi=20
def=10;
ghi=20;
tb=new Array();
// the function should now change def=1 and ghi=2
tb=firstFunction(def,ghi);

document.write("def=" + tb[0] + " ghi="+ tb[1]);
</script>

</body>
</html>

Reply Via Newsgroup a écrit:

Folks,
I have a simple script below... I come from a programming background
with PHP,C++,bash unix shell scripting so I have a rough understanding
when it comes to javascript. I have written a few javascripts of my own
but only until now have I realised that this will be the first time that
I have wanted to pass data *to* a function, chew it up, and spit it
back... In my test script below, I would have expected the script to
return def=1 and ghi=2, but it doesn't...

Can someone tell me where I've gone wrong? Thanks, all replies, via the
newsgroup would be much appreciated,

randelld
<script language="JavaScript" type="text/javascript">
function firstFunction(def, ghi)
{
def=1;
ghi=2;
return(def, ghi);
}

// create our variables
var def;
var ghi;

// set def=10 and ghi=20
def=10;
ghi=20;

// the function should now change def=1 and ghi=2
firstFunction(def,ghi);

document.write("def=" + def + " ghi="+ghi);
</script>


Jul 20 '05 #2

P: n/a
Reply Via Newsgroup <re****************@please.com> writes:
In my test script below, I would have expected the
script to return def=1 and ghi=2, but it doesn't... <script language="JavaScript" type="text/javascript">
function firstFunction(def, ghi)
{
def=1;
ghi=2;
return(def, ghi);
You can only return one value in Javascript, not a tuple.
You could return the two values as a composite value, e.g.,
an array:
return[def,ghi];
or an object:
return{def:def,ghi:ghi};

// the function should now change def=1 and ghi=2
firstFunction(def,ghi);


Javascript is call-by-value. You declare local variables with the
names "def" and "ghi" in the function (parameters are local
variables), so there is no chance of changing the value of the global
variables.

If you return an array, you could do:

var arr = firstFunction(def,ghi);
def = arr[0];
ghi = arr[1];

If you return an object, you can do:

var obj = firstFunction(def,ghi);
def = obj.def;
ghi = obj.ghi;

You could also make the function without the parameters:

function firstFunction() {
def=1; // now not a local variable, so change is to global one
ghi=2;
}
Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
G Roydor wrote:
1) il faut retourner un tableau
2) appel se fait par valretour=mafonction (.....)
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
// create our variables
var def;
var ghi;

function firstFunction(def, ghi)
{
def=1;
ghi=2;
return arguments;
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<script language="JavaScript" type="text/javascript">

// set def=10 and ghi=20
def=10;
ghi=20;
tb=new Array();
// the function should now change def=1 and ghi=2
tb=firstFunction(def,ghi);

document.write("def=" + tb[0] + " ghi="+ tb[1]);
</script>

</body>
</html>

Reply Via Newsgroup a écrit:

Folks,
I have a simple script below... I come from a programming background
with PHP,C++,bash unix shell scripting so I have a rough understanding
when it comes to javascript. I have written a few javascripts of my
own but only until now have I realised that this will be the first
time that I have wanted to pass data *to* a function, chew it up, and
spit it back... In my test script below, I would have expected the
script to return def=1 and ghi=2, but it doesn't...

Can someone tell me where I've gone wrong? Thanks, all replies, via
the newsgroup would be much appreciated,

randelld
<script language="JavaScript" type="text/javascript">
function firstFunction(def, ghi)
{
def=1;
ghi=2;
return(def, ghi);
}

// create our variables
var def;
var ghi;

// set def=10 and ghi=20
def=10;
ghi=20;

// the function should now change def=1 and ghi=2
firstFunction(def,ghi);

document.write("def=" + def + " ghi="+ghi);
</script>



Merci beaucoup!

Randell D.
Jul 20 '05 #4

P: n/a
Lasse Reichstein Nielsen wrote:
Reply Via Newsgroup <re****************@please.com> writes:

In my test script below, I would have expected the
script to return def=1 and ghi=2, but it doesn't...


<script language="JavaScript" type="text/javascript">
function firstFunction(def, ghi)
{
def=1;
ghi=2;
return(def, ghi);

You can only return one value in Javascript, not a tuple.
You could return the two values as a composite value, e.g.,
an array:
return[def,ghi];
or an object:
return{def:def,ghi:ghi};
// the function should now change def=1 and ghi=2
firstFunction(def,ghi);

Javascript is call-by-value. You declare local variables with the
names "def" and "ghi" in the function (parameters are local
variables), so there is no chance of changing the value of the global
variables.

If you return an array, you could do:

var arr = firstFunction(def,ghi);
def = arr[0];
ghi = arr[1];

If you return an object, you can do:

var obj = firstFunction(def,ghi);
def = obj.def;
ghi = obj.ghi;

You could also make the function without the parameters:

function firstFunction() {
def=1; // now not a local variable, so change is to global one
ghi=2;
}
Good luck
/L


Thanks - I think I can follow that - or at least its given me something
I can run with and research further.

Cheers
randelld
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.