+ 1
What does the "return" keyword does in the below code?
In this below code,I expect "3" as a output But I got "No output" in console Please anyone explain why this happens https://code.sololearn.com/cS5ALyo4Eq8B/?ref=app
11 odpowiedzi
+ 5
Yogeshwaran
At i == 4 you have returned so execution of flow will be stop there and it will return 0 to function main().
Instead of return use break. break will just break current iteration and you will be outside the loop but further code will be execute.
+ 4
Change return to break, idk much about C++ or C but return can exit the whole function
+ 4
If I remember correctly, the return 0 at the last line means the program runs successfully, correct me if I'm wrong because I don't have much knowledge about it
+ 3
Yogeshwaran
return is used to return value back to the function so in this case further code will not be execute.
+ 3
Yogeshwaran
Not only 0 you can return anything to the function.
Return 0 or 1 just for main function to send success and failure of the program, but return is used for other purpose also, for example suppose you have separate functionality in your code and you want to send returned value from one function to another function then we can use return.
For example:
int functionA(int a, int b) {
int sum = a + b;
return sum;
}
Now if you want to use sum in another function then you can do like this:
void functionB() {
int a = 5;
int b = 10;
int sum = functionA(a, b);
printf("%d", sum);
}
+ 2
There is no use of return in if statement . Return is used into function to store some values
+ 2
Thank you A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ ,Rushikesh ,Nick for your's responses.....
Now I understand the how return function works in my code
And I also caught that better to use break instead of return in my code..
Thanks guys(•‿•)
+ 1
Nick
your are very fast 😅
Answered at one time
+ 1
Nick you're right, if compiler find return 0 statement,then it will Return 0 to main function for stopping the execution....
You can also see the A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ comment,he also told that only
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ yes bro,now I understood.thank you very much for yours brief answer (•‿•)