Why doesn't my prototyping work?

0 Bob Jones · April 5, 2015
I basically did the same thing with Bucky except I prototyped it... Heres the code:

#include <iostream>

using namespace std;

int addNumbers();

int main()
{
    cout << addNumbers(69,41,21);
    return 0;
}

int addNumbers(int x, int y, int z)
{
    int answer = x + y + z;
    return answer;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · April 5, 2015
Please check your code for basic errors before posting. 

It doesn't work because you overloaded your function with different parameters. The prototype declaration above takes in no parameters while the addNumbers function declared below takes in 3 integers x,y,z. Prototype declarations of functions must match their implementations with the same return type and same types of arguments. 
+1 Brian Reed · April 5, 2015
Hi Bob,

You need as a minimum to declare the var-type for each argument your are going to define in a function.

 The fix:   int addNumbers(int,int,int);

So, (int, int, int) included in the prototype will make the compiler understand that these types of variables are going to be defined in the function, just as you did. The argument names are optional in the prototype and are only needed if you want to have clarity as to what they are to represent when reading the code i.e. (int age, int weight, ...etc).

As an interesting note, if you name your argument variables in the prototype you're not obliged to stick to those names when defining them later in your function.

Cheers,
Brian
+1 Stephanie Smith · April 5, 2015
Basically, when you prototyped it you didn't take into account the function prototype needs the same inputs as the overloaded function. 

instead for the prototype try this:

 int addNumbers(int x, int y, int z);
0 Saeed Jassani · April 11, 2015
Prototype should be done like this:
int addNumbers(int , int , int);
0 Eyeshield Rosalin · April 12, 2015
Maybe this code will help you


#include <iostream>

using namespace std;

int addNumbers(int tuna, int ham, int salad);

int main()
{
cout << addNumbers(69,41,21)<<endl;
return 0;
}

int addNumbers(int x, int y, int z)
{
int answer = x + y + z;
return answer;
}
  • 1

C++

107,190 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator