csmmrr04 2 Posted April 18, 2015 Share Posted April 18, 2015 Hi I someone please explain to me depsre function ' return ' in C- C++ that do not quite understand that returns... Thanks Link to comment Share on other sites More sharing options...
Grudge 5 Posted April 18, 2015 Share Posted April 18, 2015 https://msdn.microsoft.com/en-us/library/k68ktdwf.aspx Link to comment Share on other sites More sharing options...
Alina 234 Posted April 18, 2015 Share Posted April 18, 2015 return "returns" a value. Simple as that In short: You can use this to create your own functions that return a value. Simple example: int add(int a1, int a2) { return a1 + a2; } int main() { int result = add(1, 1); printf("The result is %d", result); return 0; //in main() this tells the system that the program succeeded return 1; //in main() this tells the system that the program failed } Note that return ends the function immediately. That means, that in our example, the return 1; is completely nonsense and will not be reached since the main() function already returned 0. In int main() it's important to return 0 if the program succeeds (compilers will add that tho if you miss it) and otherweise 1 for program errors. Have fun 1 Link to comment Share on other sites More sharing options...
csmmrr04 2 Posted April 19, 2015 Author Share Posted April 19, 2015 thanks So the car can only have values 1 and 0 , we understand Yes I saw that in some file is found and return false return true ... Can you explain to that? Link to comment Share on other sites More sharing options...
Premium Cataclismo 86 Posted April 19, 2015 Premium Share Posted April 19, 2015 thanks So the car can only have values 1 and 0 , we understand Yes I saw that in some file is found and return false return true ... Can you explain to that? You could better read a book. There's two types of return: int add(int a, int { return a + b; } The above return an int value, as specified in the declaration. You CAN'T return other types. void test(int a, int { int c = a + b; return; int d = c + a; } The above return will not return anything and will stop the execution of function. That means that int d = c + a; will not be executed. It will stop at return; Note: Returning a value also stops the function execution. Returning true or false is the same thing, but the declaration of your function is of type bool: bool function(int val) { if (val == 2) return false; else return true; } or simplier bool function(int val) { return (val == 2); } 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now