I'm learning C, and we have been recently been taught functions. User defined functions strike me as fascinating, and so I decided to write a simple program to try out how a function works. Here's my code:
#include"stdio.h"
int main()
{
func();
}
func()
{
printf("This program uses a function.");
}
I pressed "compile" and I got a compliler error. The exact message it gave me is:
fpermissive]
func()
^
Return code is not 0
The diagnostics do not imply a mistake in the code that I wrote, so I'm unable to find out where I made a mistake. Can someone help me fix this program, and also successfully learn how to use user defined functions?
Edit:
I had forgotten to mention the return type of the function. I did that now, and here's my modified code:
#include"stdio.h"
int main()
{
func();
return 0;
}
int func()
{
printf("This program uses a function.");
return 0;
}
I pressed compile again, and got a different error message. This one says:
was not declared in this scope
func();
^
Return code is not 0
Can someone help me?
You need to take care of two things: (a) Declaring func()
before main()
, (b) mentioning the return type of func()
.
#include<stdio.h>
void func(void); //forward declaration
int main()
{
func();
}
void func(void) //specifying return type as **void**
{
printf("This program uses a function.");
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments