Solved

Does JavaScript have input _and_ output parameter support?

Posted on 2006-10-26
6
441 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:RQuadling
  • 2
  • 2
6 Comments
 
LVL 41

Expert Comment

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

Expert Comment

by:HonorGod
Comment Utility
<!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:RQuadling
Comment Utility
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:RQuadling
Comment Utility
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
Comment Utility
Closed, 50 points refunded.
kb
Experts Exchange Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now