17
#include <stdio.h>

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

int addNumbers(int a, int b)
{
    return a + b;
}

Why does this not compile, I get a message saying implicit declaration of function addNumbers()?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
bob
  • 387
  • 2
  • 6
  • 9
  • In addition to declaring function addNumbers before main, here are my 2 cents about C style (not applicable for C++): 1) function that has no parameters should have signature (void) -- and thus `int main(void)`; 2) main should return value in C -- thus `return 0` is mandatory. – Alexander Poluektov Jan 29 '10 at 10:43
  • so how could i print it then, as when i run it it just shows nothing, i have tried changing from the main returning the sum to printf(addNumbers(a,b)); but to no avail – bob Jan 29 '10 at 10:45
  • That's another question. Also I'm sure you can find an answer if you use search on stackoverflow.com – sharptooth Jan 29 '10 at 10:47
  • Show code please, and probably you should create new question for it – Alexander Poluektov Jan 29 '10 at 10:48
  • 1
    @Alexander Poluektov: actually, C99 follows the lead of C++ and (mistakenly in my view) gives you permission not to return a value from `main()` and it is equivalent to returning 0. It is sad and not, I think, helpful for the discipline of making sure that functions that return values always return a value. – Jonathan Leffler Jan 30 '10 at 06:00
  • @JonathanLeffler - How is it that `permission not to return a value` is same as `returning 0`. First one is void and the latter one is returning an integer 0? – Nguai al May 11 '17 at 05:41
  • @Nguaial: 'Permission not to return a value' means 'no `return` statement at the end of `main`'; it does not mean 'permission to write `return;` (with no value) in a function returning an `int`'. – Jonathan Leffler May 11 '17 at 05:43
  • ... the answers to this question doesn't explain the situation very well. [Implicit function declarations in C - Stack Overflow](https://stackoverflow.com/questions/9182763/implicit-function-declarations-in-c) is better, although the specific in the question is slightly different (this one is about calling a user-defined function, the other function is about calling a built-in library function) – user202729 Feb 22 '21 at 13:29

11 Answers11

20

Either define the function before main() or provide its prototype before main().

So either do this:

#include <stdio.h>

int addNumbers(int a, int b)
{ //definition
}

int main()
{ //Code in main
  addNumbers(a, b);
}

or this:

#include <stdio.h>

int addNumbers(int, int);
int main()
{ //Code in main
  addNumbers(a, b);
}

int addNumbers(int a, int b)
{ // definition
}
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • As a side note, aren't function declarations normally done in a .h file? – Powerlord Jan 29 '10 at 14:55
  • 1
    @R. Bemrose: when there's more than one source file, yes. When there's only one source file, as here, the function should be declared as a static function (since it does not need to be accessible from any other file, since there is only the one file to compile). – Jonathan Leffler Jan 30 '10 at 06:01
7

You need to declare the function before you call it in main(). Either move it before main or at least declare it there. Also, you should prob add return 0 at the end of the main function since it's supposed to return int.

#include <stdio.h>

int addNumbers(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
    return 0;
}
rui
  • 11,015
  • 7
  • 46
  • 64
  • 1
    @ruibm: _Also, you should prob add return 0 at the end of the main function since it's supposed to return int_ : **NO** thats optional – Prasoon Saurav Jan 29 '10 at 10:45
  • 2
    Yeah, I know it is, but it is a good programming practice and the compiler will prob issue warnings if you turn on pedantic mode. – rui Jan 29 '10 at 10:49
  • Further nitpicking, in C it's a "function", not a "method". Otherwise good. – Vatine Jan 29 '10 at 10:56
6

You have to either move the entire addNumber() function above main or provide a prototype. The latter is done the following way:

int addNumbers(int a, int b);

int main()
{
//    code of main() here
}

int addNumbers(int a, int b)
{
//code of addNumbers() here
}
sharptooth
  • 167,383
  • 100
  • 513
  • 979
3

Put addNumbers before main

int addNumbers(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 4;
    int b = 3;

    addNumbers(a, b);
}

UPDATE:

To print it, printf("%i",addNumbers(a, b)); will display 7 in above case.

YOU
  • 120,166
  • 34
  • 186
  • 219
2

You can move the whole function above the point where it is called, or use a function prototype, like this:

#include <stdio.h>

int addNumbers(int a, int b); // function prototype

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

int addNumbers(int a, int b)
{
    return a + b;
}
sergiom
  • 4,791
  • 3
  • 24
  • 32
2

You'll need a forward declaration of the addNumbers function or its definition moved up before the first usage:

// 2161304
#include <stdio.h>

// forward declaration
int addNumbers(int a, int b);

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

// alternatively move this up before main ...
int addNumbers(int a, int b)
{
    return a + b;
}

Regarding main and return:

Programs will compile without. The signatures of main defined by the standard are:

int main(void)
int main(int argc, char **argv)

There are three portable return values:

0, EXIT_SUCCESS, EXIT_FAILURE

which are defined in stdlib.h.

miku
  • 181,842
  • 47
  • 306
  • 310
1

Declare the function before using it by either adding a prototype before main():

int addNumbers(int a, int b);

or move the whole addNumbers function before main().

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

I agree with declaration and definition thing but i am not getting any compilation errors with the above code.My gcc version is "4.4.1-4ubuntu9".Any ideas.

I have done a little modification to test the code.

 #include <stdio.h>

int main()
{
    int a = 4;
    int b = 3;
    printf("%d", addNumbers(a, b));   // Printf added.
}

int addNumbers(int a, int b)
{
    return a + b;
}
1

You must declare the function before using. Remember these 4 basic parts while dealing with functions.

  1. Declaration
  2. Call
  3. Definition
  4. Return
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
0

if your compiler is C99 standard it throws and error "implicit declaration", since since default promotion is obsolete in C99 standard. if you try to compile with C89 standard this would be allowable.

In C99 standard function prototype is necessary

C Learner
  • 2,287
  • 2
  • 14
  • 7
0

Since the compiler executes one line after another,by the time it sees the function call,it has to have the information about that function which the main function is calling.so my friend u need to tell the compiler about the function before you can ever use it.

Vijay
  • 65,327
  • 90
  • 227
  • 319