Athul Athul - 5 months ago 18
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????


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.