Solved

Does JavaScript have input _and_ output parameter support?

Posted on 2006-10-26
6
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

696 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