Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function without return type specified in C

I came across this piece of code in C:

#include <stdio.h>
main( )
{
 int i = 5;
 workover(i);
 printf("%d",i);
}
workover(i)
int i;
{
 i = i*i;
 return(i);
}

I want to know how the declaration of the function "workover" is valid? What happens when we don't mention the return type of a function? (can we return anything?).The parameter is also just a variable name, how does this work?

like image 439
Sanjana S Avatar asked May 30 '15 03:05

Sanjana S


1 Answers

If you do not specify a return type or parameter type, C will implicitly declare it as int.

This is a "feature" from the earlier versions of C (C89 and C90), but is generally considered bad practice nowadays. Since the C99 standard (1999) does no longer allow this, a compiler targeting C99 or later will likely give you a warning similar to the following:

program.c: At top level:
program.c:8:1: warning: return type defaults to ‘int’
 workover(i)
 ^
like image 77
Frxstrem Avatar answered Oct 24 '22 02:10

Frxstrem