473,378 Members | 1,439 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,378 software developers and data experts.

Another syntax question

I have two functions, one of which is called from within the other, which
is called from an HTML page. The called function is passed parameters that
I'd like to pass to the argument calling the second function:

HTML: onClick routine1(a,b,c);

js:

function routine1(a,b,c) {routine2(a,b); do something with a,b,c;}

The parameters a and b are not parsed by routine2(a,b). What am I doing
wrong?
--
Ed Jay (remove 'M' to respond by email)
Apr 21 '06 #1
2 1169
Ed Jay said on 22/04/2006 8:32 AM AEST:
I have two functions, one of which is called from within the other, which
is called from an HTML page. The called function is passed parameters that
I'd like to pass to the argument calling the second function:

HTML: onClick routine1(a,b,c);

js:

function routine1(a,b,c) {routine2(a,b); do something with a,b,c;}

The parameters a and b are not parsed by routine2(a,b).
I think you mean *passed* to routine2 and not passed back to routine1.
Or maybe not...

If you really mean parsed, what are their values and what should
routine2() be doing with them?

What am I doing wrong?


Not explaining your problem and not providing a simple example. ;-)

Your snippet, as posted, should be fine. But I suspect that you are
passing a and b to routines2(), doing something with them, then
expecting them to be modified in routine1() but you either aren't return
the modified values, or aren't assigning them to a and b in routine1().

From here on, I'll call them r1() and r2() to save typing.

In javascript, a function can only return a single reference. If you
want to return a number of values, you have to put them inside an object
(say an Array) and return a reference to that:

r2(a, b){
// do something with a and b
return [a, b];
}

r1(a, b, c){
var returnedArray = r2(a, b);
a = returnedArray[0];
b = returnedArray[1];
}
Note that in r2() I could have called the variables anything I like
because just the values are passed to and fro. Even if you are passing
objects, you can still call them whatever you like because references
are passed, not the actual object.
Alternatively, you can design routine2() so that it has access to
variables inside routine1():

<script type="text/javascript">

function r1(a, b, c)
{
// Declare r2 inside r1
function r2(){

// r2 has access to local variables of r1()
alert('In r2, a=' + a + ', b=' + b);

// and can modify them
a = 'freddy';
b = 'suzie';
}

// Call r2
r2();

// Show changed variable values
alert('After r2, a=' + a + ', b=' + b);
}

// Call r1
r1('fred', 'sue', 'harry');

</script>
Note that r2() has access to, and can modify, the values of variables in
r1(), also that only r1() can call r2(), it is a private method.

You can make r2() available outside r1() if you want, read Douglas
Crockford's article here:

<URL:http://javascript.crockford.com/private.html>
There is a lot of other good stuff on his site too.
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 24 '06 #2
RobG scribed:
Ed Jay said on 22/04/2006 8:32 AM AEST:
I have two functions, one of which is called from within the other, which
is called from an HTML page. The called function is passed parameters that
I'd like to pass to the argument calling the second function:

HTML: onClick routine1(a,b,c);

js:

function routine1(a,b,c) {routine2(a,b); do something with a,b,c;}

The parameters a and b are not parsed by routine2(a,b).
I think you mean *passed* to routine2 and not passed back to routine1.
Or maybe not...

If you really mean parsed, what are their values and what should
routine2() be doing with them?

What am I doing wrong?


Not explaining your problem and not providing a simple example. ;-)

Your snippet, as posted, should be fine. But I suspect that you are
passing a and b to routines2(), doing something with them, then
expecting them to be modified in routine1() but you either aren't return
the modified values, or aren't assigning them to a and b in routine1().

From here on, I'll call them r1() and r2() to save typing.

In javascript, a function can only return a single reference. If you
want to return a number of values, you have to put them inside an object
(say an Array) and return a reference to that:

r2(a, b){
// do something with a and b
return [a, b];
}


I'm not returning values to r1().
r1(a, b, c){
var returnedArray = r2(a, b);
a = returnedArray[0];
b = returnedArray[1];
}
Note that in r2() I could have called the variables anything I like
because just the values are passed to and fro. Even if you are passing
objects, you can still call them whatever you like because references
are passed, not the actual object.
Alternatively, you can design routine2() so that it has access to
variables inside routine1():

<script type="text/javascript">

function r1(a, b, c)
{
// Declare r2 inside r1
function r2(){
I don't want to declare the 2nd function within the first. I want to call
r2(a,b) from within r1(a,b,c) using a subset of the parameters passed to
r1(a,b,c).

In my situation, r2(a,b) is a lengthy and often-used subroutine. In most
cases, its use is independent of r1().
// r2 has access to local variables of r1()
alert('In r2, a=' + a + ', b=' + b);

// and can modify them
a = 'freddy';
b = 'suzie';
}

// Call r2
r2();

// Show changed variable values
alert('After r2, a=' + a + ', b=' + b);
}

// Call r1
r1('fred', 'sue', 'harry');

</script>
Note that r2() has access to, and can modify, the values of variables in
r1(), also that only r1() can call r2(), it is a private method.

You can make r2() available outside r1() if you want, read Douglas
Crockford's article here:

<URL:http://javascript.crockford.com/private.html>
There is a lot of other good stuff on his site too.


Thanks, RobG, for the detailed response. I apologize for not stating my
issue clearly. It isn't nearly as complex as you understood it to be.
Regardless, I must have had a syntax error someplace. I rewrote the
function and it works.

Real world example:

function tester1(a,b,c) {alert("f1("+a+b+c+")"); tester2(a,b);}

function tester2(a,b) {alert("f2("+a+b+")");}

Thanks again for your help.
--
Ed Jay (remove 'M' to respond by email)
Apr 24 '06 #3

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

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
2
by: Trimbitas Sorin | last post by:
Hello I have a simple syntax question : What does the following line mean: 1: %checkType; ?? I know that @test="" is an array and $test="" is a simple variable. Thank you With best regards...
3
by: gajaya1 | last post by:
I tried similar to sql server to get first 100 rows using "Select top 100 from table1" does not work? Is there another syntax?
3
by: Mike | last post by:
I have done this before in VB but I am realitively new to C#. I have a datagrid where the first bound column represents the identity column from the table. I have designated that first bound column...
3
by: Tomba | last post by:
I Get "Syntax error in query. Incomplete query clause." (3450) while running this code: strSQL = "SELECT Calendar.MyDate, Calendar.MyTime, Calendar.Currency, Calendar.," & _ "...
11
by: Sensei | last post by:
Hi again! I have ``yet another silly question'', about arrays this time. I've looked through the FAQs in the array & memory sections without finding an answer. I surely didn't look deep enough. ...
1
by: Gunawan | last post by:
Hi All, I have populate data in gridview manually. SqlCommand cmd = new SqlCommand(strSQL, cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds,...
2
by: movieking81 | last post by:
Hello All, I have a simply syntax question. I'm new to PHP, but I have worked with ASP for years. Can you reference a database field on an html page with PHP the same way you can with ASP. For...
6
by: Daniel | last post by:
I hope this question is OK for this list. I've downloaded Rpyc and placed it in my site packages dir. On some machines it works fine, on others not so much. Here is one error I get when I try...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.