By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

''warning C4715 not all control paths return a value'' What does it mean?

P: n/a
warning C4715: 'Pow' : not all control paths return a value
warning C4715: 'Sery' : not all control paths return a value

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. double Pow(double x, int n)
  5. {
  6.     if (n == 0)
  7.         return 1;
  8.     if (n > 0)
  9.         return x * Pow(x, n - 1);
  10.     if (n < 0)
  11.         return Pow(1 / x, -n);
  12. }
  13.  
  14. double Sery(int n, int t)
  15. {
  16.     if (n > 0)
  17.         return Sery(n - 1, t) + Pow(n, t);
  18. }
  19.  
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22.     std::cout << Sery(1, 1) << std::endl;
  23.         return 0;
  24. }
Nov 16 '10 #1
Share this Question
Share on Google+
2 Replies


100+
P: 207
Basically what this is telling you is that your code could potentially return garbage. Take a look at the Sery function.

Expand|Select|Wrap|Line Numbers
  1. double Sery(int n, int t) 
  2.     if (n > 0) 
  3.         return Sery(n - 1, t) + Pow(n, t); 
  4.  
  5. }
What is going to happen if n equals -1. So -1 > 0 will evaluate to false and the statement return Sery(n - 1, t) + Pow(n, t); won't be executed. This is why it is giving you the warning.
Nov 17 '10 #2

Expert 100+
P: 2,396
hype261 is right about 'Sery', but the situation is a little more confusing in 'Pow'.
Expand|Select|Wrap|Line Numbers
  1. double Pow(double x, int n) 
  2.     if (n == 0) 
  3.         return 1; 
  4.     if (n > 0) 
  5.         return x * Pow(x, n - 1); 
  6.     if (n < 0) 
  7.         return Pow(1 / x, -n); 
  8. }
The compiler is worried that the last if test might be false, causing execution to fall out of the function without hitting a return statement. You might argue that the previous two if statements guarantee that the last test can't be false, but your compiler is unable to recognize this dependency between these statements.

The following alternative will satisfy the compiler:
Expand|Select|Wrap|Line Numbers
  1. double Pow(double x, int n) 
  2.     if (n == 0) 
  3.         return 1; 
  4.     else if (n > 0) 
  5.         return x * Pow(x, n - 1); 
  6.     else   /* n must be < 0. */
  7.         return Pow(1 / x, -n); 
  8. }
By the way, you might want to change the name of your 'Pow' function so that it won't be confused with the Standard Library 'pow' function.
Nov 17 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.