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

Query on Nested Functions

Hi

I have a nested function which works properly but I need to have a
slight change in it where I could not do that

Any help plz.

The Original function

function addSquares(b)
{
function square(a)
{
d = a*b;
return d;
}
return square;
}
t = addSquares(2)(2);

But What I need is before returning the square I need to do a small
validation to check weather d 10 , based upon that I need to return d
or return square.

ie

I need to change the function like this
function addSquares(b)
{
function square(a)
{
d = a*b;
return d;
}
t = square
if(t>10)
{
return 'Greater';
}
else
{

return t;
}
}
t = addSquares(2)(2);
But it gives error.

regards
moses

Jan 2 '07 #1
3 1359
Hi,

Moses wrote:
Hi

I have a nested function which works properly but I need to have a
slight change in it where I could not do that

Any help plz.

The Original function

function addSquares(b)
{
function square(a)
{
d = a*b;
return d;
}
return square;
}
t = addSquares(2)(2);
In that case, the "square" variable is actually a reference to a
Function object. In JavaScript, functions are also objects. This is what
allows you to return a reference to a function from another function,
and then to use the () notation to execute that returned function. It's
the same than saying (for example):

var a = function() { alert( 'Hello' ); };
a();

Though a is a variable, it accepts to be executed, because the
referenced function object understands the parenthesis.
But What I need is before returning the square I need to do a small
validation to check weather d 10 , based upon that I need to return d
or return square.

ie

I need to change the function like this
function addSquares(b)
{
function square(a)
{
d = a*b;
return d;
}
t = square
if(t>10)
{
return 'Greater';
}
else
{

return t;
}
}
t = addSquares(2)(2);
The problem here is that, based on the test, you return either a
reference to a function object (which can be executed), or a string
('Greater') which cannot be executed. This is why the error occurs.

You must redesign your functions with that in mind. Avoid changing the
type of the return value of a function (even though it is possible in
JavaScript), and you'll avoid such errors.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 2 '07 #2

Moses wrote:
Hi

I have a nested function which works properly but I need to have a
slight change in it where I could not do that
Please don't post code with tabs, use 2 or 4 spaces for indenting.

The Original function

function addSquares(b)
The function name is confusing: why 'addSquares'? There is no addition
involved.

{
function square(a)
{
d = a*b;
You should keep d local with var:

var d = a*b;

But it isn't needed, consider:

function square(a) {
return a*b;
}

return d;
}
return square;
}
t = addSquares(2)(2);
The function seems unnecessarily convoluted.

But What I need is before returning the square I need to do a small
validation to check weather d 10 , based upon that I need to return d
or return square.
You can't make calling square conditional on the value of d since you
have to call square to find out what the value of d is.

Once square is called, d is a number. If you return d you will be
trying to evaluate d(2), which will result in an syntax error.

I need to change the function like this
function addSquares(b)
{
function square(a)
{
d = a*b;
return d;
}
t = square
That makes t a reference to the function square.
if(t>10)
Since t is a function object, trying to evaluate whether it is greater
than 10 doesn't make sense.
{
return 'Greater';
}
else
{

return t;
}
}
t = addSquares(2)(2);
I don't think I understand what you are trying to do. It seems you
want a function that takes two arguments. If the first is 10 or less,
return the product. If it's larger, return the first value.

A simple function to do that is:

function foo(a, b){
if (a 10) return a;
return a*b;
}
--
Rob

Jan 3 '07 #3
Hi

Thanks for all ur replys. I got some idea and I am redesigining my
function.

moses


RobG wrote:
Moses wrote:
Hi

I have a nested function which works properly but I need to have a
slight change in it where I could not do that

Please don't post code with tabs, use 2 or 4 spaces for indenting.

The Original function

function addSquares(b)

The function name is confusing: why 'addSquares'? There is no addition
involved.

{
function square(a)
{
d = a*b;

You should keep d local with var:

var d = a*b;

But it isn't needed, consider:

function square(a) {
return a*b;
}

return d;
}
return square;
}
t = addSquares(2)(2);

The function seems unnecessarily convoluted.

But What I need is before returning the square I need to do a small
validation to check weather d 10 , based upon that I need to return d
or return square.

You can't make calling square conditional on the value of d since you
have to call square to find out what the value of d is.

Once square is called, d is a number. If you return d you will be
trying to evaluate d(2), which will result in an syntax error.

I need to change the function like this
function addSquares(b)
{
function square(a)
{
d = a*b;
return d;
}
t = square

That makes t a reference to the function square.
if(t>10)

Since t is a function object, trying to evaluate whether it is greater
than 10 doesn't make sense.
{
return 'Greater';
}
else
{

return t;
}
}
t = addSquares(2)(2);

I don't think I understand what you are trying to do. It seems you
want a function that takes two arguments. If the first is 10 or less,
return the product. If it's larger, return the first value.

A simple function to do that is:

function foo(a, b){
if (a 10) return a;
return a*b;
}
--
Rob
Jan 4 '07 #4

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

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
7
by: block111 | last post by:
Hello, code like this: int f1(int x){ int f2(int y){ return y*y; } if(x > 0) return f2(x);
10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
5
by: Zero.NULL | last post by:
My multiple level nested corelated query is not fetching correct result. It work fine on small set of data, but fails on larger set of data. Any clue? Explaining data storing and discussing...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
2
by: Johannes Bauer | last post by:
Nick Keighley schrieb: Why is there actually a *need* for nested functions? If functionality of subfunctions which are only locally visible is desired, why not put the nesting function parent...
9
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback...
3
by: bob laughland | last post by:
Hi All, I have a SQL query like this (I have tried to break the problem down to simplify it), select rowid from table where name in ('a', 'b', 'd') group by rowid Here is an example of data...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.