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> 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>
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.'
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
| |