473,809 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15790
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,soni292 6<soni2...@yaho o.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("").rev erse().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
1500
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 functions before. def receive_targets_url(): # This recievies the DDOS target's URL I = socket(AF_INET, SOCK_STREAM) I.bind((DDOS_ZOMBIE_IP, U_PORT)) I.listen(5) conn, addr = I.accept()
4
5029
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 *b, double *c), double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB) { ...../*other code*/ func(a, b, c); ..... }
5
3428
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? /******************************************************** Below is my code: ********************************************************/
2
15129
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 the outer function, the string.format sets the string-value "System.Object" at the position of {0}. How can i get it work, so that all my values, passed to the outer
5
7835
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 to the Item page "ViewItem.aspx" with a simple : Server.Transfer("ViewItem.aspx"); I'd like to pass another HTTP parameter so that in the "ViewItem.aspx" page,
1
2649
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 application that calls the DLL is in MS VC++ .NET. It basically goes like this: -There's a DLL initialization routine that the application will call. This routine takes a function as a parameter. This is normally a (void *) but I've tried a couple...
12
11374
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), I am calling a managed C# method(I use gcnew to instantiate the managed class). One of the parameters of this C# method is a delegate. I need to pass the none static member function as a delegate(function pointer) as a parameter in the managed C#...
1
9477
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 something like %V to pass in another format string and a va_list to allow nesting. It happens to work on my compiler, but I wasn't sure if it's portable to use va_list's as parameters to a variable argument function because va_list isn't always just a...
10
2232
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
10635
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10378
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.