control reaches end of non-void function

plain english... 
the compiler is saying "hey i have to run some code for this function , i stop when you tell me to return something.  But im confused 
because according to your logic there  are conditions where i can go from the start to the end of this function  
and you do not tell me what to return. I need to know what to return 100% of the time and your logic does not make that clear"


every function where the return type is not void MUST return a value, period end of story.
If there are conditions ( if , else, elseif )
the logic must ALWAYS have a return statement for every possible outcome.
the logic must be stated where the compiler understands what to return for every possible scenario


int Larger(int one, int two)
this purpose of this function is to take in 2 integer values and then 
give back ( return ) which ever is the largest one. 

we will show 4 wrong examples that will result in "control reaches end of non-void function"
warnings. (these are actually errors in these functions) dispite being labeled as warning. That is 
why we compile to include all warnings  

Then there are 3 correct ways to do this function.
example 1 File: main1.cpp


#include "cmpslib19.h" // all the special functions provided for this class



int Larger(int one, int two)
{


if ( one > two)
	return one;

/*
  what if one is NOT > two
  it has reached the end of the function, there is no return statement
*/
}


int main()
  {
  int a = 100;
  int b = 200;

   int largest = Larger(a,b);

   cout << largest << " is the largest value of " << a << " and " << b << endl;

  return 0;
  }


g++ -Wall  -o runme1 -D LOGGING_ON main1.cpp 
main1.cpp: In function 'int Larger(int, int)':
main1.cpp:15:1: warning: control reaches end of non-void function [-Wreturn-type]


here are 2 more version of the function that suffer from the same logical error

example 2
File: main2.cpp 


#include "cmpslib19.h" // all the special functions provided for this class



int Larger(int one, int two)
{


if ( one > two)
	return one;

if ( one <  two)
	return two;
/*
  what if one ==  two
  it has reached the end of the function, there is no return statement
*/
}


int main()
  {
  int a = 100;
  int b = 200;

   int largest = Larger(a,b);

   cout << largest << " is the largest value of " << a << " and " << b << endl;

  return 0;
  }

g++ -Wall  -o runme2 -D LOGGING_ON main2.cpp 
main2.cpp: In function 'int Larger(int, int)':
main2.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
g++ -Wall -o runme2 -D LOGGING_ON main3.cpp main3.cpp: In function 'int Larger(int, int)': main3.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
example 3
File: main3.cpp 


#include "cmpslib19.h" // all the special functions provided for this class



int Larger(int one, int two)
{


if ( one > two)
	return one;

else if ( one <=  two)
	return two;
/*
  this one has the correct complarison operators so tht if one ==  two there is a condition for it BUT
  it is still structured in a way that will generate the same error. The logic is still not formatted in a way where the compiler thinks
  all scenarios are handled

*/
}


int main()
  {
  int a = 100;
  int b = 200;

   int largest = Larger(a,b);

   cout << largest << " is the largest value of " << a << " and " << b << endl;

  return 0;
  }

the problem with all the functions so far is that the logic is incomplete 

all the return statemens are linked to a condition ( if, if else ... )
and there are not written properly so that NO MATTER what values are passed in 
the function will have a return statement for that condition


int Larger ( int one, int two)
the function MUST return a value.
it will be either one or two

there are 3 possible conditions
one < two
one > two
one == two
here we have conditions for all 3 scenarios but the logic is not correct
be using 3 if condtions the compiler is not able to determine if we have all the scenarios met
as far as it is concerned all 3 tests could be false.. 
it is not smart enough to analize the tests and determine that those tree tests will cover 100% of the time
we need to use an else or simply always end the function with a return statemen that has no condtions on it


example 4 File: main4.cpp


#include "cmpslib19.h" // all the special functions provided for this class



int Larger(int one, int two)
{


if ( one > two)
	return one;

if ( one <  two)
	return two;

if ( one ==  two)
	return two;


/*
  even though we have conditions for all 3 possible scenarios our logic is not correct
  the compiler is not smart enough to know that there is no way all three tests cant be false

  you need to use an else or change the tests to make it so that the compiler knows
*/
}


int main()
  {
  int a = 100;
  int b = 200;

   int largest = Larger(a,b);

   cout << largest << " is the largest value of " << a << " and " << b << endl;

  return 0;
  }

HERE ARE A COUPLE POSSIBLE SOLUTIONS TO THIS 
WHERE THERE IS ALWAYS A RETURN STATEMENT FOR EVERY CONDITION
example 5 File: main5.cpp


#include "cmpslib19.h" // all the special functions provided for this class



int Larger(int one, int two)
{


if ( one > two)
	return one;
else 
    return two;



/*
  there are ONLY 2 scenarios 
  either you return one or 
  you return two

  there is no possible way to get to the end of the function 
  without a return statement
  
  the logic states exaclty what to do 
  this works 100% of the time
*/
}


int main()
  {
  int a = 100;
  int b = 200;

   int largest = Larger(a,b);

   cout << largest << " is the largest value of " << a << " and " << b << endl;

  return 0;
  }

example 6 File: main6.cpp


#include "cmpslib19.h" // all the special functions provided for this class



int Larger(int one, int two)
{


if ( one > two)
	return one;

if ( one <  two)
	return two;

// if ( one ==  two) 
	return two;


/*
here is example 4 with the last test commented out
now if we get past the first two tests we will always return a value

there is no way to reach the end of the funtion without reaching a return statemen

furthermore if the first two condtiions where not true one and two must be equal
so why would we bother to test it,  not only is it unnecessary but it creates logic that makes the compiler think that
there is a scenaio where there is not a value to return
*/


}


int main()
  {
  int a = 100;
  int b = 200;

   int largest = Larger(a,b);

   cout << largest << " is the largest value of " << a << " and " << b << endl;

  return 0;
  }

example 7 File: main7.cpp


#include "cmpslib19.h" // all the special functions provided for this class



int Larger(int one, int two)
{
int val = two;

if ( one > two)
	val = one;


return val;
 
/*
here we store the value we would like to return in a temporary value
we can have all the test we want but at the very end of the funtion 
we tell it what to return 

*/
}


int main()
  {
  int a = 100;
  int b = 200;

   int largest = Larger(a,b);

   cout << largest << " is the largest value of " << a << " and " << b << endl;

  return 0;
  }