![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 22
| return something in a void 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();
}
|
| vrkiller is offline | |
| | #2 |
| 3735928559 Join Date: Mar 2008
Posts: 662
| you don't. you change the return type |
| m37h0d is offline | |
| | #3 |
| Registered User Join Date: Feb 2009
Posts: 22
| how cna you give me a clue? |
| vrkiller is offline | |
| | #4 |
| 3735928559 Join Date: Mar 2008
Posts: 662
| Code: int calculator()
{
//...
}
|
| m37h0d is offline | |
| | #5 |
| Registered User 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;
}
|
| QuestionKing is offline | |
| | #6 |
| Registered User Join Date: Feb 2009
Posts: 22
| oh thx you, can you return a string to?`;D |
| vrkiller is offline | |
| | #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 | |
| | #8 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| 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 | |
| | #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 | |
| | #10 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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 | |
| | #11 |
| Registered User Join Date: Feb 2009
Posts: 22
| thx ;D. Thread complete and closed |
| vrkiller is offline | |
| | #12 |
| 3735928559 Join Date: Mar 2008
Posts: 662
| you should really do the tutorials. |
| m37h0d is offline | |
![]() |
| Tags |
| return, void |
| Thread Tools | |
| Display Modes | |
|
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 |