Athul Athul - 4 months ago 11
C++ Question

Function to find largest number

I'm learning C++ through Sololearn. Below is a code to find the largest of two numbers.

#include <iostream>
using namespace std;

int max(int a, int b){

if (a > b) {
return a;
}

return b;
}

int main() {

cout << max(7, 4) << endl;
return 0;
}


Result - 7

But shouldn't it return b also since there's return b in function????

Answer

Only one return statement will execute within a function. As soon as the code encounters the first return it will immediately leave the function and no further code will execute.

Comments