C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2009, 05:33 PM   #1
Registered User
 
Join Date: Feb 2009
Posts: 22
return something in a void

well my question is:

if i want to return something from a void how do i do that like:
Code:
void calculater()
{
cout << "5+5=";
return 10;
}
int main()
{
int number=calculater();
cout <<number;
getchar();
}
how?
vrkiller is offline   Reply With Quote
Old 02-10-2009, 05:42 PM   #2
3735928559
 
Join Date: Mar 2008
Posts: 662
you don't. you change the return type
m37h0d is offline   Reply With Quote
Old 02-10-2009, 05:46 PM   #3
Registered User
 
Join Date: Feb 2009
Posts: 22
how cna you give me a clue?
vrkiller is offline   Reply With Quote
Old 02-10-2009, 05:49 PM   #4
3735928559
 
Join Date: Mar 2008
Posts: 662
Code:
int calculator()
{
//...
}
m37h0d is offline   Reply With Quote
Old 02-10-2009, 05:50 PM   #5
Registered User
 
QuestionKing's Avatar
 
Join Date: Jan 2009
Posts: 27
You can not do that.

void specifically means no return

if you wan't to return an integer, just declare your function to return an integer:
Code:
int MyFunction()
{
    cout << "My Statement";
    return 10;
}
Also please indent code when you post it.
QuestionKing is offline   Reply With Quote
Old 02-10-2009, 05:52 PM   #6
Registered User
 
Join Date: Feb 2009
Posts: 22
oh thx you, can you return a string to?`;D
vrkiller is offline   Reply With Quote
Old 02-10-2009, 06:00 PM   #7
Super Moderator
 
Join Date: Sep 2001
Posts: 4,680
You can return a pointer to a char, or you could accept a pointer as an argument and manipulate that string.
sean is offline   Reply With Quote
Old 02-10-2009, 06:21 PM   #8
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
Quote:
Originally Posted by vrkiller View Post
oh thx you, can you return a string to?`;D
Yes:
Code:
std::string MyFunction()
{
    return "Some String";
}
__________________
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
cpjust is offline   Reply With Quote
Old 02-10-2009, 06:24 PM   #9
Registered User
 
Join Date: Feb 2009
Posts: 22
ok well then how do i recieve my things i have returned?

man i maybe like this forum
vrkiller is offline   Reply With Quote
Old 02-10-2009, 06:26 PM   #10
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by vrkiller View Post
ok well then how do i recieve my things i have returned?

man i maybe like this forum
x = function().

As long as x is of the same type as what function returns, or it is something that can be automatically converted (e.g. int to float, float to int, char * to string) it shouldn't cause any problem.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 02-10-2009, 06:35 PM   #11
Registered User
 
Join Date: Feb 2009
Posts: 22
thx ;D. Thread complete and closed
vrkiller is offline   Reply With Quote
Old 02-10-2009, 07:25 PM   #12
3735928559
 
Join Date: Mar 2008
Posts: 662
you should really do the tutorials.
m37h0d is offline   Reply With Quote
Reply

Tags
return, void

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I make this code more elegant? ejohns85 C++ Programming 3 04-02-2009 08:55 AM
How to better manage large .cpp files 39ster C++ Programming 6 08-25-2008 08:24 AM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
ChangeDisplaySettings - Blank? Tonto Windows Programming 13 12-26-2006 04:17 PM
i cannot see the mouse arrow peachchentao C Programming 6 12-10-2006 04:14 AM


All times are GMT -6. The time now is 05:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22