By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,678 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,678 IT Pros & Developers. It's quick & easy.

Passing a function as an argument and using the evaluated functionas an argument

P: n/a
Since you can pass a function to a ... erm...... function....

how to you use the result of a function as the argument for another
function instead of passing the actual function to it.

i.e.

function foo2(){}

function foo(func){}
foo(foo2);
This passes foo2 to foo, can I do foo(foo2()) ?

i.e. is the difference between whether a function is passed or evaluated
dependent on whether you use "()" after the function name?
Nov 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
User1014 wrote:
Since you can pass a function to a ... erm...... function....

how to you use the result of a function as the argument for another
function instead of passing the actual function to it.

i.e.

function foo2(){}

function foo(func){}
foo(foo2);

This passes foo2 to foo, can I do foo(foo2()) ?
Yes you can do that. An argument to a function call is an expression
that is evaluated prior to passing the resulting value as the actual
argument. Functions calls are themselves expressions so they are fine
in an arguments list.

Whether the resulting code would be as clear is it could be is another
question though.
i.e. is the difference between whether a function is passed or evaluated
dependent on whether you use "()" after the function name?
Yes, a function name is an identifier and evaluates as a reference to
the function object. A Parenthesise following a function name (with
arguments or not) act as an operator that evaluates the Identifier to
its left to the value of a reference to a function object and then
operates upon that value by executing the function.

Whatever expression you provide as an argument is fully evaluated
before the resulting value is passed as an argument.

Richard.

Nov 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.