473,799 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to pass a function as an argument to another function

Hi,

I want to pass my function

myFunc('a', 'b', 'c')

as an argument to another function. However, this will not work

doStuff('x', 'y', myFunc('a', 'b', 'c'))

because the expression "myFunc('a' , 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?

Thanks for any help , - Dave

Jan 31 '07 #1
8 2332
Lee
la***********@z ipmail.com said:
>
Hi,

I want to pass my function

myFunc('a', 'b', 'c')

as an argument to another function. However, this will not work

doStuff('x', 'y', myFunc('a', 'b', 'c'))

because the expression "myFunc('a' , 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?

Thanks for any help , - Dave
pass the function and the arguments, separately:

dostuff('x','y' ,myFunc,'a','b' ,'c');

function dostuff(a1,a2,f ,b1,b2,b3) {
// do something with f(b1,b2,b3)
}
--

Jan 31 '07 #2
On Jan 31, 12:44 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
laredotorn...@z ipmail.com said:
Hi,
I want to pass my function
myFunc('a', 'b', 'c')
as an argument to another function. However, this will not work
doStuff('x', 'y', myFunc('a', 'b', 'c'))
because the expression "myFunc('a' , 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?
Thanks for any help , - Dave

pass the function and the arguments, separately:

dostuff('x','y' ,myFunc,'a','b' ,'c');

function dostuff(a1,a2,f ,b1,b2,b3) {
// do something with f(b1,b2,b3)

}

--
Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?

Thanks, - Dave

Jan 31 '07 #3
On Jan 31, 5:37 pm, "laredotorn...@ zipmail.com"
<laredotorn...@ zipmail.comwrot e:
On Jan 31, 12:44 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
laredotorn...@z ipmail.com said:
>Hi,
>I want to pass my function
>myFunc('a', 'b', 'c')
>as an argument to another function. However, this will not work
>doStuff('x', 'y', myFunc('a', 'b', 'c'))
>because the expression "myFunc('a' , 'b', 'c')" gets evalauted before
>it is passed to the function "doStuff". Does this make sense?
>Thanks for any help , - Dave
pass the function and the arguments, separately:
dostuff('x','y' ,myFunc,'a','b' ,'c');
function dostuff(a1,a2,f ,b1,b2,b3) {
// do something with f(b1,b2,b3)
}
--

Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?

Thanks, - Dave
Have you tried passing in an object that has myFunc as one of it's
methods?

Feb 1 '07 #4
Lee
la***********@z ipmail.com said:
>
On Jan 31, 12:44 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
>laredotorn...@ zipmail.com said:
>Hi,
>I want to pass my function
>myFunc('a', 'b', 'c')
>as an argument to another function. However, this will not work
>doStuff('x', 'y', myFunc('a', 'b', 'c'))
>because the expression "myFunc('a' , 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?
>Thanks for any help , - Dave

pass the function and the arguments, separately:

dostuff('x','y ',myFunc,'a','b ','c');

function dostuff(a1,a2,f ,b1,b2,b3) {
// do something with f(b1,b2,b3)

}

--

Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?
If it's expecting a function, it's not expecting that function be provided with
arguments. Your other option is to create a wrapper function to be passed to
doStuff(), and have that wrapper function call myFunc() with your arguments:

function wrapper() {
return myFunc('a','b', 'c');
}

doStuff('x','y' ,wrapper);
--

Feb 1 '07 #5
On Feb 1, 8:37 am, "laredotorn...@ zipmail.com"
<laredotorn...@ zipmail.comwrot e:
On Jan 31, 12:44 pm, Lee <REM0VElbspamt. ..@cox.netwrote :
laredotorn...@z ipmail.com said:
>Hi,
>I want to pass my function
>myFunc('a', 'b', 'c')
>as an argument to another function. However, this will not work
>doStuff('x', 'y', myFunc('a', 'b', 'c'))
>because the expression "myFunc('a' , 'b', 'c')" gets evalauted before
>it is passed to the function "doStuff". Does this make sense?
>Thanks for any help , - Dave
pass the function and the arguments, separately:
dostuff('x','y' ,myFunc,'a','b' ,'c');
function dostuff(a1,a2,f ,b1,b2,b3) {
// do something with f(b1,b2,b3)
}
--

Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?
Yes - pass a function object like:

soStuff( a, b, c, function(){myFu nc(arg1, arg2, arg3);} )

e.g.

function myFunc (arg1, arg2){
alert('myFunc called with ' + arg1 + ' and ' + arg2);
}

function doStuff (arg1, arg2, fn) {
alert('About to call myFunc from doStuff');
fn();
}

function bar(){
doStuff ('one', 'two', function(){myFu nc ('bar1','bar2') ;});
}

bar();

--
Rob
Feb 1 '07 #6
la***********@z ipmail.com a écrit :
Hi,

I want to pass my function

myFunc('a', 'b', 'c')

as an argument to another function. However, this will not work

doStuff('x', 'y', myFunc('a', 'b', 'c'))

because the expression "myFunc('a' , 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?
Q&D, not tested, but this should get you started:

partial = function(f, a, b, c) {
return function() {
return f(a, b, c);
};
}

doStuff('x', 'y', partial(myFunc, 'a', 'b', 'c'));

HTH
Feb 1 '07 #7
dd
On Jan 31, 6:41 pm, "laredotorn...@ zipmail.com"
<laredotorn...@ zipmail.comwrot e:
I want to pass my function
myFunc('a', 'b', 'c')
as an argument to another function. However, this will not work
doStuff('x', 'y', myFunc('a', 'b', 'c'))
I'm sure I'll get shouted at for using it, but here's how I
solved that problem:

doStuff('x', 'y', "myFunc('a' , 'b', 'c')" )

When the time came, inside doStuff, to make the
call, I just eval'ed the 3rd param:

function doStuff(p1, p2, p3) {
bla,bla,bla
eval(p3);
}

If 'a', 'b' and 'c' are not fixed values but are variables
instead, then breaking open the double-quoted string
let's you get at the values:

"myFunc("+a+"," +b+","+c+")"

If any of them are strings, then you need extra quotes
to ensure the eval doesn't think they're variables, eg:

"myFunc('"+a+"' ,'"+b+"','"+c+" ')"

I think people give eval too much bad press. It can get
you around some difficult problems like this one. Sure
if a proper solution comes up then switch to that, but
as long as you don't have one, this can keep your code
at least doing what it should.

Feb 1 '07 #8
dd
On Feb 1, 10:55 am, "dd" <dd4...@gmail.c omwrote:
When the time came, inside doStuff, to make the
call, I just eval'ed the 3rd param:
Of course if myFunc was meant to form a closure then
this is unsuitable. I'm assuming it's a function with
global scope.

Feb 1 '07 #9

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

Similar topics

2
2401
by: Satish Chimakurthi | last post by:
Hi all, My question is surely a basic one, but somehow, I am not able to figure it out. I have a python file "satish.py" as follows: *satish.py* def main(): y()
1
2539
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could show me a example of how to do this. I was told to pass the arrays from function to function, but i do not know how .... Thanxs if you can help. My code (First two functions only - there's more):
39
6558
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
4
1501
by: deanfamily | last post by:
Here's the problem: I have a function that references another one in this manner: class circleType: public pointType so, pointType is redefined by circleType, thereby giving circleType access to all the functions in pointType if need-be. Here is the function I am using in pointType: void pointType::setCoords(double xCoord, double yCoord) { pointX = xCoord;
2
9590
by: Edlueze | last post by:
Greetings: I have two functions and I would like to pass the ParamArray gathered from one function to the other function. For the purposes of this post, let's say that they are calculating averages (they're actually processing a sequence of pairs of variants and the sequence is of unknown length). I want something like these two functions:
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); ..... }
1
1523
denizvb
by: denizvb | last post by:
Hai programmers, Do you know How to write a function inside another function in c
1
1506
by: krishnasamy | last post by:
Hi, I am retrieving the image from DLL through vc++ dll. Actually Image is retrieving in Callback function of main function. When I writing that image file inside of the Callback function then no problem in retrieving. At the same I want to send the Imagedata to the another function and write the same which cause the error. Here I given that code,
3
4148
by: jbanas | last post by:
In MS Access i have a function that is calculating the distance between two locations using the latitude and longitudes for two points assuming the earth is flat and the lat and long are perpendicular. I called that function distance. My issue in moving forward is taking the distance i am given from the above function and bringing it into another function i call function wp. I would like to say if the distance is less than 300 then calculate...
4
15790
by: soni2926 | last post by:
hi, is it possible to pass a function into another function as a parameter? Say i have these: function SaveMe(text) {...} function SaveMeNow(text) {...}
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10225
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
9072
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...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.