Mohit Singla
Mohit Singla

Reputation: 101

Functions in C language

This code is working good.

#include<stdio.h>
#include<conio.h>
void show()
{
    printf("Show function");
}
void main()
{
    show();
}

but this requires some more.

#include<stdio.h>
#include<conio.h>
void main()
{
    show();
}
void show()
{
    printf("Show function");
}

That is.. I have to declare a "Show" function in main. why i need to do this?

Upvotes: 0

Views: 114

Answers (6)

Because in genuine recent C every function has to be declared or defined (perhaps in some header file) before being used.

So add a line (declaring show) like

void show(void);

before your main in the second example. It is not needed in the first example because show is defined before its first use.

Notice that <conio.h> is not a standard header (don't exist on POSIX systems)

Upvotes: 0

S.I.J
S.I.J

Reputation: 989

Actually you are not declaring the show() function in main but you are calling the show() function from main. A function has to be declared outside of the main function or before calling it. Function declaration is required when you define a function in one source file and you call that function in another file. In such case you should declare the function at the top of the file calling the function.

(Your program may run with a warning of function not declared)

#include <stdio.h>

void show();

void main()
{
      show();
}

void show()
{
      printf("Show function");
}

Upvotes: 0

Mario
Mario

Reputation: 36487

Your code is parsed from beginning to end in one pass.

The second snippet won't compile since the compiler doesn't know what show () is once hitting the call.

As a fix, you can use a simple forward declaration, telling the compiler the function's name and signature without telling it about its body straight away:

#include <stdio.h>
#include <conio.h>

void show(); // Now the compiler knows the function

void main()
{
    show();
}

void show()
{
    printf("Show function");
}

Such declarations typically happen within header files. The actual implementation may then be out into any translation file (usually grouped as well).

Upvotes: 1

Christopher Ian  Stern
Christopher Ian Stern

Reputation: 1412

In the first example, when C enounters the function call to show, it already knows what arguments the function takes, because the definition of show is above the call in the file, and C just goes thru once top to botom. In the second example the when call to show is hot C hasn't yet heard anything about a function named show.

Upvotes: 1

Niclas Lindstedt
Niclas Lindstedt

Reputation: 104

You can declare the function before main like this (or inside a header file):

#include <stdio.h>
#include <conio.h>

void show();

void main() {
    show();
}

void show() {
    printf("Show function"

Upvotes: 0

asimes
asimes

Reputation: 5894

You need to declare show before main because main calls show before it is defined. You can do this with a function prototype as shown below

#include <stdio.h>

// You reference show in main but the compiler will see main before show
// This is required to let the compiler know show will later be defined
void show();

void main()
{
    show();
}

void show()
{
    printf("Show function");
}

Upvotes: 2

Related Questions