0

Return

when i try to delete the return result in the example code, it's still give the right answer. So anyone can show me how the 'return' exactly work and when I have to declare return?

24th Sep 2017, 2:48 PM
Afif Humaidi
Afif Humaidi - avatar
10 Antworten
+ 5
@Afif Humaidi Im sorry, I wasn't able to help much.
24th Sep 2017, 3:54 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
This is a weird phenomenon. The most probable explanation is that the function is supposed to return an int, and, so, it returned the last int value from the EAX return register. https://stackoverflow.com/questions/4644860/function-returns-value-without-return-statement https://stackoverflow.com/questions/4260048/c-function-defined-as-int-but-having-no-return-statement-in-the-body-still-compi
24th Sep 2017, 3:15 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
Can you post the code? Ill explain using that.
24th Sep 2017, 3:04 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
this is correct code.. what do u want to do ? if u want to does not have any return value use this #include <iostream> using namespace std; void addNumbers(int x, int y) { int result = x + y; // return result; } int main() { // cout << addNumbers(50, 25); } Notice : of course you can not use cout on void type of function
24th Sep 2017, 3:11 PM
Akbar Khalilzadeh
Akbar Khalilzadeh - avatar
24th Sep 2017, 3:14 PM
Akbar Khalilzadeh
Akbar Khalilzadeh - avatar
+ 2
thanks guys
24th Sep 2017, 3:53 PM
Afif Humaidi
Afif Humaidi - avatar
+ 1
what is your code?
24th Sep 2017, 3:04 PM
Akbar Khalilzadeh
Akbar Khalilzadeh - avatar
+ 1
#include <iostream> using namespace std; int addNumbers(int x, int y) { int result = x + y; return result; } int main() { cout << addNumbers(50, 25); }
24th Sep 2017, 3:06 PM
Afif Humaidi
Afif Humaidi - avatar
+ 1
No. You help me enough :) @Kinshuk Vasisht
24th Sep 2017, 3:56 PM
Afif Humaidi
Afif Humaidi - avatar
0
there's still result 75
24th Sep 2017, 3:13 PM
Afif Humaidi
Afif Humaidi - avatar