LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-12-2006, 03:13 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
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.
 
Old 05-12-2006, 03:23 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 46
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.

Last edited by spooon; 05-12-2006 at 03:25 PM.
 
Old 05-12-2006, 04:11 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129
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
 
Old 05-12-2006, 05:17 PM   #4
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Ah, good old function pointers, what could be simpler? Use them all the time. The easiest way to handle the syntax is to use a typedef.
 
Old 05-12-2006, 09:26 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,450

Rep: Reputation: 63
From what i can tell from the post im not sure function pointers is what hes after.

I think what you mean is use a function that returns a value and use its return as a data value of another function.

like this
Code:
#include<stdio.h> 

int getnum(void);
void printnum(int); 

int main() 
{
   printnum(getnum()); 
 return 0; 
 
}
int getnum(void)
{
   int numb; 

   numb = 4; 

   return numb; 
}
void printnum(int number) 
{
  printf("%d",number);
}
mabye ?

Last edited by exvor; 05-13-2006 at 12:36 AM.
 
Old 05-12-2006, 11:52 PM   #6
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
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?
 
Old 05-13-2006, 12:34 AM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,450

Rep: Reputation: 63
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.
 
Old 05-13-2006, 01:04 AM   #8
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
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.
 
Old 05-13-2006, 04:39 AM   #9
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
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));
}

Last edited by ioerror; 05-13-2006 at 04:40 AM.
 
Old 05-16-2006, 02:16 PM   #10
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,951

Rep: Reputation: 158Reputation: 158
um, hello
Code:
printf("hello my name is %s", getname());
printf is a function defined in stdio.h
 
Old 05-16-2006, 03:44 PM   #11
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
This is an example about nested function calls, as in foo(bar()), hence the trivial code.

P.S. And it's C++, not C.
 
Old 05-18-2006, 10:55 AM   #12
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,951

Rep: Reputation: 158Reputation: 158
i apologize for being obnoxious,

thanks for the correction io.

______________

to further clarify in words:

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.

Last edited by schneidz; 05-18-2006 at 11:00 AM.
 
Old 05-19-2006, 02:29 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware64 13.37, Kubuntu 10.04
Posts: 2,944

Rep: Reputation: Disabled
I use templates; that way you don't have to get the pointer type exactly, and it also works for functions with default arguments:
Code:
#include <iostream>
#include <string>

void ShowMe1(const std::string &sShow)
{ std::cout << sShow << "\n"; }

int ShowMe2(const char *sShow1, const char *sShow2 = "\n")
{
        std::cout << sShow1 << sShow2;
        return 0;
}


template <class Function>
void ShowAnything(const char *sShow, Function *fFunction)
{ (*fFunction)(sShow); }


int main()
{
        ShowAnything("Hello, this is ShowMe1", &ShowMe1);
        ShowAnything("Hello, this is ShowMe2", &ShowMe2);
        return 0;
}
ta0kira

PS This will also work for functors; classes which have an 'operator ()'.

Last edited by ta0kira; 05-19-2006 at 02:32 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM
function defn inside a function in C...!!! arunka Programming 1 02-05-2006 01:08 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM


All times are GMT -5. The time now is 03:19 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration