Common Function Errors
There are many common mistakes which beginning programmers
make when writing functions. New ones are introduced with
the new features of C++.
We've already seen some of them
- Ambiguous function names when attempting to overload a function
- Use of a for-loop variable outside the for-loop body
Some of the other common errors are
shown below. Some are logical errors, some are syntactical.
A swap( ) function that doesn't
Consider the following code that calls the function swap( )
that is intended to exchange the parameters passed to it.
What is the error?
//-------------------------------
// File: swap.cpp
// Author: D. Frey
// Date: 9/10/03
// Section: 123
// Project: none
// This program shows an inadvertent function
// error. There is no compiler error, but the
// code doesn't work as advertised
#include
using namespace std;
// prototypes for functions found in this file
void swap (int a, int b);
int main ( )
{
int int1 = 42, int2 = 36;
cout << "Before calling swap( )\n"
<< "\tint1 = " << int1 << endl
<< "\tint2 = " << int2 << endl;
// swap the valuess in int1 and int2
swap( int1, int2 );
cout << "After calling swap( )\n"
<< "\tint1 = " << int1 << endl
<< "\tint2 = " << int2 << endl;
return 0;
}
//------------------------------
// Function: swap()
// PreConditions:
// none
// PostConditions:
// the values in the parameters are exchanged
//-----------------------------
void swap (int a, int b)
{
int temp = a;
a = b;
b = temp;
}
Non-void function error
This error is common in C as well as C++.
What does the error mean? How do we fix iyt?
//-------------------------------
// File: nonvoid.cpp
// Author: D. Frey
// Date: 9/10/03
// Section: 123
// Project: none
// This program shows the common compiler error
// "nonvoid function does not return a value"
//------------------------------------------
#include
using namespace std;
// prototypes for functions in this file
bool IsOld (int yourAge, int AARPAge);
int main ( )
{
int myAge = 42;
int oldAge = 60;
if (IsOld (myAge, oldAge))
cout << "You are old!\n";
return 0;
}
//--------------------------
// IsOld
// PreConditions:
// both age parameters are non-negative
// PostConditions
// returns true if yourAge is greater
// than the designated oldAge limit
//----------------------------------
bool IsOld (int age, int oldAge)
{
if (age > oldAge)
return true;
}
nonvoid.cpp: In function `bool IsOld(int, int)':
nonvoid.cpp:42: warning: control reaches end of non-void function
A typo when calling the function
#include
using namespace std;
int AddOne( int n );
int main ( )
{
int x = 42;
cout << Addone ( x ) << endl;
return 0;
}
int AddOne (int n)
{
return n + 1;
}
results in this error
BadFuncName.cpp: In function `int main()':
BadFuncName.cpp:6: `Addone' undeclared (first use this function)
BadFuncName.cpp:6: (Each undeclared identifier is reported only once for each
function it appears in.)
Too many or too few arguments
With not enough arguments as in this code
#include
using namespace std;
int Add( int a, int b);
int main ( )
{
int x = 42;
int y = Add ( 66 );
cout << y << endl;
return 0;
}
int Add (int n, int m)
{
return n + m;
}
we get errors like this
MissingArg.cpp: In function `int main()':
MissingArg.cpp:4: too few arguments to function `int Add(int, int)'
MissingArg.cpp:8: at this point in file
With too many arguments as in this code
#include
using namespace std;
int Add( int a, int b);
int main ( )
{
int x = 42;
int y = Add ( 66, x, 87);
cout << y << endl;
return 0;
}
int Add (int n, int m)
{
return n + m;
}
we get this error
TooManyArg.cpp: In function `int main()':
TooManyArg.cpp:4: too many arguments to function `int Add(int, int)'
TooManyArg.cpp:8: at this point in file
Passing the wrong type of argument
#include
using namespace std;
int Add( int n, int m);
int main ( )
{
double x = 42.999;
int y = Add ( 66, x);
cout << y << endl;
return 0;
}
int Add (int n, int m)
{
return n + m;
}
results in these WARNINGS (not errors)
BadParamType.cpp: In function `int main()':
BadParamType.cpp:7: warning: passing `double' for argument passing 2 of `int Add(int,
int)'
BadParamType.cpp:7: warning: argument to `int' from `double'
Missing Prototype
When you use a function before the compiler has seen its
prototype as in this code
#include
using namespace std;
int main ( )
{
int x = 42;
int y = Add ( 66, x);
cout << y << endl;
return 0;
}
int Add (int n, int m)
{
return n + m;
}
you get errors like this
MissingPrototype.cpp: In function `int main()':
MissingPrototype.cpp:8: `Add' undeclared (first use this function)
MissingPrototype.cpp:8: (Each undeclared identifier is reported only once for
each function it appears in.)
MissingPrototype.cpp: In function `int Add(int, int)':
MissingPrototype.cpp:16: `int Add(int, int)' used prior to declaration
Last Modified: Monday, 28-Aug-2006 10:16:00 EDT