Hi there again :) Could someone tell me what is the difference between function declare in main() and outside main() ? Because I don't see anything wrong.
Thanks so much in advance.
Here is example,

#include <stdio.h>

//void fun(int x,int y, int z);

int main()
{
void fun(int x,int y, int z); //Function inside main
int a,b,c;

printf("...");
scanf("...",...);

printf("...");

fun(x,y,z);

getchar();

return 0;
}

void  fun(int x,int y, int z)
{
........
printf("");

}

Just scope, that's all.

Inside main, only main() can call it.
Unless you copy/paste the prototype into other functions which need to call fun()

Thanks so much.

I don't know about others but to me putting function declarations inside main is just like a potential time-bomb, even if at this moment your functions do not call each other.

Hi there again :) Could someone tell me what is the difference between function declare in main() and outside main() ? Because I don't see anything wrong.
Thanks so much in advance.
Here is example,

#include <stdio.h>

//void fun(int x,int y, int z);

int main()
{
void fun(int x,int y, int z); //Function inside main
int a,b,c;

printf("...");
scanf("...",...);

printf("...");

fun(x,y,z);

getchar();

return 0;
}

void  fun(int x,int y, int z)
{
........
printf("");

}

H!
It's a concept of local and global declaration. If u declare a function inside main() then that function is local to main() means the function is accessed by only main() and it's life time is also bounded to main i.e. until main() exist.
But if you declare the same function outside the main() then it will act as global declaration. Such a function can be accessed by all functions in your program and it's life time is limited to a program i.e. until program exist(not a single function).
That's the difference of declaring function outside main()(global declaration) and inside main()(local declaration).

I don't know about others but to me putting function declarations inside main is just like a potential time-bomb, even if at this moment your functions do not call each other.

Oh, rubbish! Your description is excessive. The worst thing that can happen is that another function needs to call the declared function, and a compiler error occurs unless the function declaration is moved (to file scope) or replicated in the other function. A compiler error that can be eliminated with a simple copy or move of a line of code is hardly catastrophic, so does not warrant being described as a potential time-bomb.

Oh, rubbish! Your description is excessive. The worst thing that can happen is that another function needs to call the declared function, and a compiler error occurs unless the function declaration is moved (to file scope) or replicated in the other function. A compiler error that can be eliminated with a simple copy or move of a line of code is hardly catastrophic, so does not warrant being described as a potential time-bomb.

It's up to individual's interpretation of what constitutes to a potential time-bomb. To a novice programmer, if he makes a habit of declaring functions inside main function because all the functions do not call each other and the program compiles properly, he may run into unwarranted compiler errors in future if the program expands. That's a bad practice that needs to nip it in the bud. It's easy to solve of course, but in the first place this problem could be avoided.

Oh, and I really think you are rude to describe other's comments as rubbish. That's plain rude and uncalled for.

Oh, and I really think you are rude to describe other's comments as rubbish. That's plain rude and uncalled for.

It is equally rude to provide misinformation, and describing a trivial concern as a timebomb is misinformation - and, worse, misinformation that can deceive beginners. I certainly agree it is better not to place function declarations inside the scope of functions, but the consequences are pretty insignificant. Compiler errors (and handling them) are part of the normal learning process by which beginners build knowledge.

It is equally rude to provide misinformation, and describing a trivial concern as a timebomb is misinformation - and, worse, misinformation that can deceive beginners. I certainly agree it is better not to place function declarations inside the scope of functions, but the consequences are pretty insignificant. Compiler errors (and handling them) are part of the normal learning process by which beginners build knowledge.

Read my statement again. I said "TO ME, putting function declarations inside main is just like a potential time-bomb, even if at this moment your functions do not call each other". Its a PERCEPTION. Its an OPINION. I don't think it's what you call a misinformation. You can say that you don't agree with my analogy, but you can say it in a less rude way.

Anyway, its really pointless to argue over this. Let's move on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.