Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a function that returns int not need a prototype?

Tags:

c

I tried this program on DevC++ that would call two different types of function, one returning int and one returning char. I'm confused why the int function doesn't need a prototype, while the char one and any other type of function does.

#include <stdio.h>

//int function1();
char function2() ;

int main (){
    int X = function1() ;
    char Y = function2() ;
    
    printf("%d", X) ;
    printf ("%c", Y) ;
    
    return 0 ;
}

int function1(){
    return 100 ;
}

char function2(){
    return 'B' ;
}

The output:

100B

If I remove the prototype for the char function, it results in:

[Error] conflicting types for 'function2'
[Note] previous implicit declaration of 'function2' was here
like image 987
louisnot Avatar asked Apr 18 '21 14:04

louisnot


People also ask

Do you always need a function prototype?

It is not required, but it is bad practice not to use prototypes. With prototypes, the compiler can verify you are calling the function correctly (using the right number and type of parameters).

Do all functions require a function prototype?

Yes, every function must have a prototype, but that prototype may appear either in a separate declaration or as part of the function's definition.

Why do you need a function prototype?

The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it.

Do you need to have function prototypes in C++?

The compiler does not find what is the function and what is its signature. In that case, we need to function prototypes. If the function is defined before then we do not need prototypes.


3 Answers

In the old days of C any function that was not declared explicitely was supposed to return int type when you call it. If the compiler then finds the function implementation and sees an int return type, everything is fine. If the function returns anything else than int you get the error message as you saw with the second function.

This implicit int type declaration was removed from the standard with C99. Now you should at least get a warning from your compiler when you use a function without prototype.

If you did not get any diagnostic message for first funcion, you should turn up warning level in your compiler or switch to at least C99 mode instead of ancient versions.

Edit: Empty parameter lists in funcion definitions is a deprecated feature as well. You should not use it. Always provide prototype for every function with return type and parameter list.

like image 177
Gerhardh Avatar answered Nov 15 '22 06:11

Gerhardh


If a function is used before it is declared, the usage becomes an implicit declaration of the function. When a function f is implicitly defined, the definition given to it is int f(), i.e. a function which accepts an unspecified number of arguments and returns an int.

This implicit definition of a function matches the actual definition of function1 but not function2. So calling function1 this way gives no error but attempting to call function2 this way results in the implicit definition not matching the actual definition, giving an error.

This behavior goes back to pre-standardized versions of C where all objects (and a function's return type) had a default type of int if not declared. This was still present in the C89 standard but removed in the C99 standard, although some compilers such as gcc still support this obsolescent usage as an extension.

like image 20
dbush Avatar answered Nov 15 '22 05:11

dbush


It's just an ancient relic from when C was first designed. It was actually removed as early as C99, but many compilers still support this type of declaration. But it's not recommended to use it.

I'm not sure if there were any real rationale behind it, but C was heavily inspired by the language B. And in B you did not have to specify the return type for functions. That actually made perfect sense, because there was only one type, word.

In the same way you did not have to specify the type of variables either. You only specified if it had automatic or static storage. And that's where the completely useless keyword auto in C comes from. It does not mean the same as in C++. It just means "not static".

like image 24
klutt Avatar answered Nov 15 '22 05:11

klutt