You are here:

C++/Using function, result is incorrect


Hello.   I am very new to C++.  We're on our 9th day, and learning Functions.   I am listing the problem, and the codes I wrote.  My codes compiles with no problem, but it doesn't seem to be picking up the function I used.   I would appreciate any suggestions.   Thank you.  


Write a function called isValid that determines whether any 3 values can represent the sides of a triangle. The main function should prompt a user to enter 3 sides of a triangle, call isValid and output valid or invalid. The isValid function should have 3 parameters and return a bool. Given sides a, b, c; if a + b > c and a + c > b and b + c > a, then the sides represent a valid triangle.


#include <iostream>
using namespace std;

int isValid (int, int, int);

int main ()
int a;
int b;
int c;

// Prompt the user to enter 3 values
cout << " Enter the 1st value:    ";
cin >> a;
cout << " Enter the 2nd value:    ";
cin >> b;
cout << " Enter the 3rd value:    ";
cin >> c;  

system ("pause");
return 0;

} //end main
// The Function Main is executed, then calls the isValid function:

int isValid (int tri_a, int tri_b, int tri_c)  

 // Determine if the user inputs are valid.
   if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
    cout << "Valid" << endl;
    cout << "Invalid" << endl;
} // end isValid  

You have missed one very important point about functions:

The code they contain is not executed until they are called from somewhere else.

Whether this is because you really did not grasp this point or because you just forgot to put in a call to isValid in your code I cannot say!

If you do not see the point that functions need to be called to be executed consider: if this were not so when would you expect your isValid function’s operations to be executed? Before the main function? After the main function? Neither would make sense. If you execute isValid before main then you have no values to check. If you execute isValid after main you again are performing these operations at the wrong point, as the values you had are destroyed (local variables are destroyed when they go out of scope, which is at the end of your functions in this case) and you have already paused execution of main to see the result!

In fact it is very odd that your main function as posted in your question has a great space between asking for and obtaining the values for a, b and c and making the call to system("pause"). It seems you may then have just forgotten to add in the call to isValid in main that takes a, b, and c as arguments.

Oh, you do not return anything from isValid but you specify the function as returning an int. This is not valid and your compiler should have picked it up as an error. If you do not return anything from a function in C++ you should specify the return type as void:

   void isValid (int tri_a, int tri_b, int tri_c);

   // ...

   void isValid (int tri_a, int tri_b, int tri_c)
   // ...

However isValid would be more useful if it only reported whether the parameter values form a valid triangle and leave the decision on what to do with this information up to the caller – not all such requests would require the result displayed on the console – not all programs have a console in any case! In this case the bool C++ type would be a good choice. In fact this is what you have been asked to do:

   “The main function should prompt a user to enter 3
    sides of a triangle, call isValid and output valid
    or invalid. The isValid function should have 3
    parameters and return a bool.”

So thing to take away from this are:

   - Functions have to be called to be executed.

   - If your code is not behaving correctly then
     there is a reason for it and it is nearly always
     your fault! So go back over your code carefully
     to find what is wrong.

   - Always read the specification carefully (in this
     case that is the homework question you have been set).
     It is not good wasting time writing the wrong thing.

Note that code not working (or even compiling) the first time (or second, third time) and having to be corrected (or debugged) is a standard part of software development. Please get used to having to go through this cycle...


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.