help!

"no previous prototype for function 'addition'" it doesn't stop the program from running successfully, but i just want to know what it means for future reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
#include <iostream>
using namespace std;

int addition (int o,int t)
{
    
    int s;
    s = o+t;
    return (s);
    
    
}



int main (int argc, const char * argv[])
{

    int twonumbers;
    int onenumber;
    cout << "enter a number";
    cin >> onenumber;
    cout << "and another";
    cin >> twonumbers;
    cout << "and now...";
    sleep(1);
    cout << "the sum of those two numbers is" << addition(onenumber,twonumbers);

return 0;
}

It's an overly pedantic warning.

A function prototype allows you to declare that a function exists without actually giving it a body. For example... this would be a prototype for your addition function:

1
2
int addition (int o,int t); // <- note the semicolon
 // <- and no {braces} or body 


To eliminate the warning you could put the prototype before the function definition. Though it's really unnecessary.

Personally... I would ignore the warning and even consider tweaking build options to lower the warning level. That warning is pretty absurd, IMO.
I personally want to know what compiler provided that warning so I can contact the creator for an interview.

+1 to Disch
That warning is pretty absurd, IMO.
Last edited on
Warnings are (or should be) for detecting potential logical problems in code that may compile but might not do what the developer intended.

The worst thing that can happen if you don't prototype a function is you'll try to call it and it won't be declared. In which case you'll get a compiler error telling you.

I cannot imagine any situation where this would manifest into a runtime bug. Therefore issuing a compiler warning literally has no value.

So yeah... completely absurd. Only sense I can make of it is someone was trying to push some kind of coding standard on the user ("all functions must have prototypes!!!")
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
void foo(bool value){
   std::cout << ( value? "Yes": "No" ) << '\n';
}
int main(){
   foo("hello world");
}

void foo(const char *message){
   std::cout << message << '\n';
}
Yes
Last edited on
That example makes sense, but I still fail to see why the warning appeared for the OP's code.
ne555: a contrived example, but a valid one.

I guess the warning is not as useless as I originally thought. Though I'm still not sure if I agree with it.
man wrote:
-Wmissing-prototypes (C and Objective-C only)
Warn if a global function is defined without a previous prototype declaration.
This warning is issued even if the definition itself provides a prototype. Use
this option to detect global functions that do not have a matching prototype
declaration in a header file. This option is not valid for C++ because all
function declarations provide prototypes and a non-matching declaration will
declare an overload rather than conflict with an earlier declaration. Use
-Wmissing-declarations to detect missing declarations in C++.


Edit: The example is plain bad
Having void foo(bool); would make the warning go away.

Edit: Nope, bool and overloads don't exist in C
Last edited on
Topic archived. No new replies allowed.