Athul Athul - 1 year ago 76
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download