Solved

Does JavaScript have input _and_ output parameter support?

Posted on 2006-10-26
6
447 Views
Last Modified: 2008-02-01
Hi.

<?php
function aa(a,b,c)
      {
      c = a + b;
      return a - b;
      }

var j = 0;
alert(aa(1,2,j));
alert(j);
?>

What happens at the moment is I have an alert of -1 and then 0.

How do I update j?

I can't use j in the function as that restricts it to a single variable.

In PHP I would ...

<?php
function aa($a, $b, &$c)
 {
 $c = $a + $b;
 return $a - $b;
 }
$j = 0;
$x = 0;
$k = aa(1, 2, $j);
$l = aa(3, 5, $x);
?>

sort of thing.

Similar facilities exist in VB and Delphi. Just can't seem to work out the JavaScript way.

Ta!
0
Comment
Question by:Richard Quadling
  • 2
  • 2
6 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 17810885
 Javascript parameters are pass by value.  So, you would need to have the function return a result
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17810903
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head>
<title> function parms </title>
</head>
<body>
<script type='text/javascript'>
  function inc( x ) {
    x++;
  }
  var i = 0;
  document.write( 'Before: ' + i );
  inc( i );
  document.write( ' After: ' + i );
</script>
</body>
</html>
----------------------------------
Output is:
----------------------------------
Before: 0 After: 0
0
 
LVL 40

Author Comment

by:Richard Quadling
ID: 17811137
Not quite.

I need to modify a value which is passed as well as generate a result.

I'm using a class_param to do this now. I was hoping for a byref mechanism.

0
 
LVL 40

Author Comment

by:Richard Quadling
ID: 17811408
function class_Param(m_InitValue)
      {
      this.value = m_InitValue;
      }


i_magic_value = new class_Param(0);

can then use

x(i_magic_value);

and x can write to i_magic_value.value

Sorted!
0
 
LVL 1

Accepted Solution

by:
kodiakbear earned 0 total points
ID: 17886466
Closed, 50 points refunded.
kb
Experts Exchange Moderator
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question