Link to home
Start Free TrialLog in
Avatar of Richard Quadling
Richard QuadlingFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Does JavaScript have input _and_ output parameter support?

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!
Avatar of HonorGod
HonorGod
Flag of United States of America image

 Javascript parameters are pass by value.  So, you would need to have the function return a result
<!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
Avatar of Richard Quadling

ASKER

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.

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!
ASKER CERTIFIED SOLUTION
Avatar of kodiakbear
kodiakbear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial