Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / November 2006



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
User1014 - 30 Nov 2006 11:29 GMT
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?
Richard Cornford - 30 Nov 2006 12:13 GMT
> Since you can pass a function to a ... erm...... function....
>
[quoted text clipped - 10 lines]
>
> 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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.