How do I run a function within a function arguement? C/C++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
How do I run a function within a function arguement? C/C++
I have been wanting to implement a function within a function arguement lately for a couple reasons. Example would be:
void Function1(int 1, int 2, Function2){ //do something }
But as far as I know you have to declare the variable type in the arguement before you can pass anything. If it makes it easier, Function2 is just a getline function. i.e. getline(myfile,someline);
I'm trying to get information from a text file into a part of my program/game that displays fonts onto a graphical window. Instead of ints and chars, what would I put in place of Function2 when void Function is made? What should I do?
--EDIT--
I have seen programs use the return function. Such as this found at cplusplus.com's tutorial...
// function example
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
is that what I would have to do, something similar to this? Though it still does not answer my question as to what to initialize my function arguement as.
Last edited by RHLinuxGUY; 05-12-2006 at 03:16 PM.
Google for "function pointers". You can pass a pointer of Function2 as an argument to Function1; but you need to know the types and stuff of Function2 beforehand, and the actual syntax for function pointers is a little tricky.
I am not really sure what you are trying to do though; so can you explain what you want to do more clearly; because function pointers are not usually used that often.
You can certainly pass a function (function pointer) to another function but as spoon suggests they are tricky and you *probably* don't need it. Because they are difficult to implement correctly one really good resource which I use is the C++FAQ
Thank you exvor, that is what I was looking for... though the example code you supplied didn't work for me, I got the basic idea and made something very similar to that:
Code:
#include <iostream>
using namespace std;
int Func1(void)
{
int a;
a = 3;
return a;
}
void Func2(int Value)
{
cout<<Value<<endl;
}
string Func3(void)
{
string a = "hello";
return a;
}
void Func4(string Value)
{
cout<<Value<<endl;
}
int main()
{
if(Func1()==2)
{
cout<<"It is correct!\n";
}
else
{
cout<<"It is not!\n";
}
Func2(Func1());
Func4(Func3());
return 0;
}
Strangely, if I make Func3 return a char (thats saying I make it char Func3(void)) it cannot return chars... why?
In your example function 3 is supposed to return a string. Actually it gets real complicated if you attempt to return a string this way i belive you need like a pointer to a pointer and such. I generally avoid them. Good news is you dont have to do it this way you can just make the string outside of the function and pass it by refrence to the function and after the function is done modifying the information it will stay there.
only other thing is you would need to make it an array instead of a constant string.
Ohh reason my code above dident work is cause it is C and needs to be compiled with gcc also i was missing a , in there as well. I fixed it i hope.
Yes, I cought the comman (,) but it still would spit garbage out after I compiled it.. successfully. :> Thank you again though, I'll look into the string problem you brought up to me. Would I get the same problem with chars? Or should I just stick to what you said about have an external (I'm assuming global, or a pointer) string initialized/defined somehwere else.
Ah, nested function calls. Yeah, you don't need function pointers for that, should have read the post more closely.
Your string problem is that you're returning a local variable which no longer exists when the function returns. You're probably better off defining the string in main and passing it as an argument to Func3.
Also, as exvor pointed out, you don't really want to return a 'string' in Func3, better to return a pointer or reference. Well, in C you'd use a pointer, with C++, a reference is probably easier.
So, something like this:
Code:
string& Func3(string& a)
{
a = "hello";
return a;
}
int main()
{
string a;
...
Func4(Func3(a));
}
a function can be used as transparently as a variable as long as it returns something and that something is in the format that the other function is expecting.
it's a shorthand way of declaring a new local variable, storing the return of a function to that local variable then passing that variable to another function.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.