Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

Why is a function prototype not needed in C if there is no return in a function defined after main()?

$
0
0

As expected following code generates an error if prototype double cubenum(); is not declared as required in C.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Answer is: %f", cubenum(3.0));
    return 0;
}

double cubenum(double number){
    double result = number * number * number;
    return result;
}

Whereas if cubenum definition is above is replaced with following definition without return then it does not generate any error when cubenum prototype is not declared:

void cubenum(double number){
    double result = number * number * number;
    printf("Answer is: %f", result);
}

And when prototype is declared as void cubenum(); with above cubenum definition without return it generates following error:

||=== Build: Debug in xxx(compiler: GNU GCC Compiler) ===|
C:\xxx\main.c||In function 'main':|
C:\xxx\main.c|10|error: invalid use of void expression|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Line 10 was when tested: printf("Answer is: %f", cubenum(3.0));

So, question is:

Why a function which does not have a return, prototype declaration is not required and if declared gives error in the above example?

GCC version info

gcc (MinGW.org GCC-6.3.0-1) 6.3.0

Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>