473,498 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing to/from function problems


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
4 1355
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
7591
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
2
3007
by: Manish | last post by:
Hi, I m facing a problem in re-writing a code from VB to C #. We are using a third party DLL to develop a application of Fax. The current application is already written in VB and we have to...
5
1880
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
4
5911
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
6
3873
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.