Function as parameter of otehr function

Hello!
Please,what is wrong in that code?

Te parameter of fu2 should be resultof fu1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int fu1(int v){
int z=v+2;
return z;
}

int fu2(int fu1(int v)){
int g=fu1(int v)+2;
return g;
}


int main(){
int a=2;
cout<<fu2(a);
return 0;
}
 



Output: 
1
2
3
 In function 'int fu2(int (*)(int))':
Line 7: error: expected primary-expression before 'int'
compilation  



Many thanks!!!
Are you trying to have fu2 take different functions at runtime? If not, just have it take a normal integer parameter. You seem to have some misconceptions here.
Hell!
if a=2,

fu1 should count a+2=4

fur2 is ment to count result from a fu1, but writen in a function form.
so, the fu2 should return 6.
That's not what I asked, reread my post.
If you want to pass a function, look up std::function . That's not necessary for what you're trying to do however.

From what I can tell you'd be better off having fu2 just take regular int as argument, then when you call it inside main let fu1 return the value being passed in. Pretty sure L B was trying to tell you the same thing.
Hello!
U probably ment this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int fu1(int v){
int z=v+2;
return z;
}

int fu2(int k){
int g=fu1(k)+2;
return g;
}


int main(){
int a=2;
cout<<fu2(a);
return 0;
}


Output:
1
6


But, my original q was, if we can use function expression instead of an integer (result-> return)
morning,
you could use std::function:
http://en.cppreference.com/w/cpp/utility/functional/function

or the 'old way' would be to use function pointers:
http://www.cprogramming.com/tutorial/function-pointers.html

But both Austin and LB have already stated this...
You're not explaining very clearly what it is you want to do. I can see two possibilities:

1) You want to define fu2 to take an integer argument. When you call fu2, you want to pass in as that argument, the result of a call to fu1.

2) You want to define fu2 such that, inside fu2, it calls another function. You want it to be able to call different functions at different times, so you're passing in as an argument the identity of the function you want it to call. So, on an occasion where you want fu2 to call fu1, the you call fu2 and pass in a pointer to fu1 so that, inside fu2, it calls fu1.

Which one of those is it that you want?
if you want to pass a functions a parameter do it like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int fu1(int v){
int z=v+2;
return z;
}

int fu2(int fu1(int v), int k){
int g=fu1(k)+2;
return g;
}


int main(){
int a=2;
cout<<fu2(fu1, a);
return 0;
}
The int parameter must be passed as well
Topic archived. No new replies allowed.