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

pass function into another function as parameter?

hi,
is it possible to pass a function into another function as a
parameter? Say i have these:

function SaveMe(text)
{...}

function SaveMeNow(text)
{...}

function WhichToSave(x, y, z)
{...}

a button will call WhichToSave, that function will perform some logic
and then call one of the Save methods, can i pass that method in as
say parameter z and have it just call the function right away, passing
one of the SaveMe methods a parameter which is determined by the
WhichToSave method?

Thanks.
Nov 21 '08 #1
4 15767
On Nov 20, 3:57*pm, soni2926 <soni2...@yahoo.comwrote:
hi,
is it possible to pass a function into another function as a
parameter? *Say i have these:

function SaveMe(text)
{...}

function SaveMeNow(text)
{...}

function WhichToSave(x, y, z)
{...}

a button will call WhichToSave,
How does a button call something? Is WhichToSave a callback from an
event handler attached to a button, as in:-

button.onclick = WhichToSave;

?
that function will perform some logic
and then call one of the Save methods, can i pass that method in as
say parameter z and have it just call the function right away, passing
one of the SaveMe methods a parameter which is determined by the
WhichToSave method?
Functions can be passed.

function invoker(f) {
f();
};

function a() {
document.title = "a";
}

invoker(a);

Garrett
Thanks.
Nov 21 '08 #2
On Nov 20, 9:57*pm, soni2926 <soni2...@yahoo.comwrote:
hi,
is it possible to pass a function into another function as a
parameter?
Yes.

function foo(){
alert('foo');
}

function bar(fn){
fn();
}

bar(foo); // alerts 'foo'

--
Gabriel Gilini
Nov 21 '08 #3
On Nov 20, 7:13*pm, Gabriel Gilini <gabr...@usosim.com.brwrote:
On Nov 20, 9:57*pm,soni2926<soni2...@yahoo.comwrote:hi,
is it possible to pass a function into another function as a
parameter?

Yes.

function foo(){
* *alert('foo');

}

function bar(fn){
* *fn();

}

bar(foo); // alerts 'foo'

--
Gabriel Gilini
thank you!
Nov 21 '08 #4
On Nov 20, 5:57*pm, soni2926 <soni2...@yahoo.comwrote:
hi,
is it possible to pass a function into another function as a
parameter?
function reverse_str( s )
{ return s.split("").reverse().join("")
}

function upcase( s )
{ return s.toUpperCase()
}

funcs = [reverse_str, upcase]
for (var i = 0; i<funcs.length; i++)
document.write( "<p>" + funcs[i]( "was i able" ) )
Nov 21 '08 #5

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

Similar topics

5
by: Bart Nessux | last post by:
I have 2 functions among others. One gets a URL and returns its, For example, it returns 'http://127.0.0.1' How can I pass this to another function? I've never worked with code that has lots of...
4
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
5
by: Julien C. | last post by:
Hi all, I have an "EditeItem.aspx" page which lets me edit properties of an "Item". In the OnClick() event of my Save button, I do save Item changes to the database and then I redirect the user...
1
by: NorrYtt | last post by:
I am having a problem with passing a function as a parameter to my DLL so it can call it back with updates. The DLL is written in LabWindows CVI with some #ifdef 'C'-style wrappings. The...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
10
arunmib
by: arunmib | last post by:
hi all, I just got this freaky kind of doubt....I have the following piece of code, int main() { int Val= 10, *ptr; ptr = &Val; TestFn(&Val);
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.