473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to return values from a java script function

I'd like to write a function 'double_values' which doubles two values.

var value1 = 5;
var value2 = 20;

alert("1) Value 1: " + value1 + " / Value 2: " + value2);
double_values(value1, value2);
alert("4) Value 1: " + value1 + " / Value 2: " + value2);

...

//-------------------------------------------------------

function double_values(a, b) {
alert("2) A: " + a + " / B: " + b);
a = a * 2;
b = b * 2;
alert("3) A: " + a + " / B: " + b);
}
Output:
1) Value 1: 5 / Value 2: 20
2) A: 5 / B: 20
3) A: 10 / B: 40
4) Value 1: 5 / Value 2: 20
I'm wondering why the output 4) shows
4) Value 1: 5 / Value 2: 20
and not
4) Value 1: 10 / Value 2: 40

What do I have to do to return both values a & b to value1 & value2?
Stefan
Jun 8 '06 #1
2 1466
On Thu, 08 Jun 2006 10:41:38 +0200, Stefan Mueller wrote:
I'd like to write a function 'double_values' which doubles two values.

var value1 = 5;
var value2 = 20;

alert("1) Value 1: " + value1 + " / Value 2: " + value2);
double_values(value1, value2);
alert("4) Value 1: " + value1 + " / Value 2: " + value2);

...

//-------------------------------------------------------

function double_values(a, b) {
alert("2) A: " + a + " / B: " + b);
a = a * 2;
b = b * 2;
alert("3) A: " + a + " / B: " + b);
}
Output:
1) Value 1: 5 / Value 2: 20
2) A: 5 / B: 20
3) A: 10 / B: 40
4) Value 1: 5 / Value 2: 20
I'm wondering why the output 4) shows
4) Value 1: 5 / Value 2: 20
and not
4) Value 1: 10 / Value 2: 40

What do I have to do to return both values a & b to value1 & value2?
Stefan


The problem is that in javascript function parameters are passed by value,
so the variable inside the function is a copy of the original variable,
not the variable itself.

One way round this is to use an object, like this (not tested):

var obj={val1:5, val2:20};
alert("val1:"+val1+" val2:"+val2);
doubleValues(obj);
alert("val1:"+val1+" val2:"+val2);

function doubleValues(object) {
object.val1*=2;
object.val2*=2;
}

In this case, 'obj' and 'object' are both pointers to an object that is
created by the {val1:5,...} line, and kept in memory until all the
pointers to it have been reassigned.

Your example is like if you give someone (jim) a piece of paper with a
number on it, but when you give it to them they copy the number onto their
own piece of paper. Jim doing maths on his piece of paper won't change
what's on yours.

The object example is more like this:
- first you ring fred (the object) and give them two numbers.
- then you give jim (the function) a piece of paper with fred's phone
number on it (the pointer). He copies it down.
- then he rings fred, asks for the numbers, doubles them, and tells fred
the answer.
- when you ring fred again, you'll get the new numbers.

I hope this is helpful rather than patronising...
Jun 8 '06 #2
Many thanks for your help and explanation. It works great.
Stefan
Jun 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the...
2
by: Zhichun Pu | last post by:
Hi guys, I can't seem to get anything returned from functions. If I use the <script src = ... > command, nothing works. But if I define the functions locally within the file, then it works. ...
0
by: Mircea Pleteriu | last post by:
Hi all, I have created a .NET Windows control with c#. I have placed the control on a HTML page within the OBJECT element. Everything works fine up to now. Now, I wanna implement the...
4
by: Don Grover | last post by:
I hope some one can help, I have a html table that is created with asp that has a row of repeated buttons down the side. these call a page passing a query string with invoice number. I need to...
4
by: randy.p.ho | last post by:
Using JDBC, is there a way to call a stored procedure with multiple return values? Thanks.
4
by: ACaunter | last post by:
Hi there, Is there a way to call a javascript function and return its value, from inside an asp.net button ?? -- AdamPC@hotmail.com
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
3
by: toffee | last post by:
Hi all, I got a pre-formatted spreadsheet. would it be possible using js to copy the data from a table on the current webpage, open the spreadsheet and paste the content ? if so, anyone got any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.