2

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?

Pritt Balagopal
  • 1,476
  • 2
  • 18
  • 32
  • What is the return type of your function `func()`? You need to mention that such as `void func()` or `int func()` – Aditi Rawat Nov 05 '17 at 07:04
  • Okay, lemme fix that @AditiRawat – Pritt Balagopal Nov 05 '17 at 07:04
  • you need to declare `func()` before `main()` – sorush-r Nov 05 '17 at 07:05
  • Also you need to take care of forward declaration – Aditi Rawat Nov 05 '17 at 07:05
  • You are using an incomplete function prototype. Traditionally, this meant it returns an `int` and its arguments are unspecified, but you really shouldn't do that today. If you don't want to return a value, then declare it void. Similarly, if you don't want to pass any arguments, declare a void argument list. I.e. `void func(void)`. Finally, add a forward declaration for the function so that the compiler knows how to call it. All of this should have been covered in your tutorial. – Tom Karzes Nov 05 '17 at 07:06
  • @TomKarzes Wonderful, thanks. You explained everything that I needed to know. Can you convert that comment into an answer so I can accept it as one? – Pritt Balagopal Nov 05 '17 at 07:15

5 Answers5

4

You need to study the syntax better. Your function doesn't return anything, so you need to put a void in front of the name. You also need to put a void between parenthesis of your functions. And move the function definition before the main, or, as other answers mention, add a function declaration before the use of the variable.

A function declaration is composed by:

  1. the optional keyword extern
  2. the first line of the function, i.e. the type returned, the name, parameters between parenthesis
  3. a semicolon

In your case: extern void func(void);, or simply void func(void);.

Costantino Grana
  • 3,132
  • 1
  • 15
  • 35
  • *"And move the function definition before the main."* Ahh I had been thinking about that for quite a while. My CS teacher put the function definition after the main(), so I had thought we could do it after as well. Anyway, thanks for patching this up. – Pritt Balagopal Nov 05 '17 at 07:11
  • Or add a forward declaration, which is a good idea in general. You don't need to move the definition. – Tom Karzes Nov 05 '17 at 07:11
  • My personal preference for students is to start by saying that what you use has to be defined before its use. After some practice than introduce declarations. – Costantino Grana Nov 05 '17 at 07:16
2

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.");
}
Aditi Rawat
  • 784
  • 1
  • 12
  • 15
1

The error occurred because the compiler didn't encounter any function named

func();

when it entered the main function. You need to, atleast, declare a function if not writing its body.

So, you have to do declare a function before main if you wan to write its code after main()

aditya rawat
  • 154
  • 10
0

You are missing return type of func(). Please look at syntax of way of defining functions.

Prem
  • 51
  • 4
0
#include"stdio.h"

void func();  // Forward declare func() here.

int main()
{
    func();
}

void func() // Ensure func() has a return type 'void'
{
    printf("This program uses a function.");
}
Stewart
  • 4,356
  • 2
  • 27
  • 59