June 2025
Intermediate to advanced
1093 pages
33h 24m
English
You have already seen return as a statement to exit the program from main. Besides main, you can use it to exit any function you write.
// https://godbolt.org/z/5ddxa9v1j#include <iostream> // coutint min3(int x, int y, int z) { // function returns an int if(x<y) { if(x<z) return x; else return z; } else if(y<z) { return y; } else return z;}void printMin(int x, int y, int z) { // function returns nothing if(x<0 || y<0 || z<0) { std::cout << "Please only numbers greater than 0\n"; return; } std::cout << min3(x,y,z) << "\n";} // no return hereint main() { printMin(3, -4, 8); printMin(6, 77, 4); return; // special return in main}
Listing 8.21 With “return”, the current function is exited. If necessary, provide ...
Read now
Unlock full access