LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2006, 03:45 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
Function Declaration in C


I was wondering what is the difference between putting a declaration for a C function inside of main versus putting it below include statement (or in a header if it is in a separate source file). Thanks.
 
Old 10-08-2006, 05:59 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
i dont know C, but, i dont think you can declare a function inside of another, ie:
Code:
#include <stdio.h>
int main()
{
    int some_func()
    {
       return 1;
    }

}
if you put the function AFTER main() then you must have a function prototype (before main) for it so the compiler knows what the function is when it gets called, ie:
Code:
#include <stdio.h>

int some_func(); // <-- this is a 'prototype', telling the compiler
// that this function exist, what its expected parameters are (none)
// and its return type (int) so it knows if a compile-time error occurs
// ie, if the function was called as: some_func(1);
//                                               ^

int main()
{
   return some_func();
}

int some_func() //function implementation (body/details)
{
   return 1;
}
remember the file is processed sequentially so a program like this wont work (because the compiler wont know the some_func() function exist (because, sequentially, it hasnt been defined yet):
Code:
#include <stdio.h>

int main()
{
   return some_func();
}

int some_func()
{
   return 1;
}
or you can simply declare the function instead of doing the prototype before main, as in:
Code:
#include <stdio.h>

int some_func()
{
   return 1;
}

int main()
{
   return some_func();
}
i dont know 100% but i think the purpose of function prototyping is so that other programmers reading your code will be able to (quickly, easily) see each function you create, their return type, parameters/signature, and name. they wont have to scroll through the file to see the implementation (body) of the function.. it might be useful to comment each function at the top next to the prototype to make things even more clear, ie:

Code:
#include <stdio.h>

int some_func();
/*
some_func() - example function that does fun stuff etc
parameters  - none
return type - int -- explain what each return value means to the program/mer
*/

int main()
{
   return some_func();
}

int some_func()
{
   return 1;
}
sorry for lengthy reply for a simple question.. got carried away. hope it helps

Last edited by nadroj; 10-08-2006 at 06:06 PM.
 
Old 10-08-2006, 11:09 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Declaring all your functions at the top of your program allows the single pass compiler to know all the function definitions before they are called. If the functions are not defined ahead of the call either by self prototyping (having the functions in the correct order) or by forward deceleration you'll get an error.

Using header files has multiple benefits. It allows the code to be split into multiple files that can be compiled separately. Without the headers the functions in other files would have to be "included" in its entirety. Method body and all, the pre-compiler would assemble all the .cpp files into a single file and throw it at the compiler. Using headers also allows the code to be published as a binary library, not reveling the implementation. Just publish the header (API) and supply the library; the compiler uses the header and passes the objects compiled and the library to the linker. Another forgotten benefit is dependencies. On large projects where compile time is extensive, 95% of the changes are all in the implementation and not the method signatures, so changes can be made to the .cpp files and not force other objects to be rebuilt.
 
Old 10-09-2006, 02:42 AM   #4
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
Hi,

I guess you guys didn't read my question properly. I know about separate compiling, header files and all that stuff. My question was:

How is it different putting a function declaration (NOT definition, just a declaration statement) inside of main, from putting it at the top of the source file (which can be through #include, if separate compilation, or explicitly)?
 
Old 10-09-2006, 08:03 AM   #5
silent_cutthroat
LQ Newbie
 
Registered: Nov 2005
Distribution: Arch
Posts: 27

Rep: Reputation: 15
For C++ then the declaration is only available in main, for example this is an error:
Code:
int main()
{
    extern void f();
    f();
    return 0;
}

void g()
{
    f();
}
In C you don't need to declare a function before use it and you can't overload functions, so every declaration of f no matter where (in the same translation unit) must be the same. In C this example is an error too because in g the compiler (GCC ) sees f(); as declaration for int f(); (implicit int) which conflicts with void f(); in main.

GCC has a neat extension for nested functions (but not closures).

Last edited by silent_cutthroat; 10-09-2006 at 08:08 AM.
 
Old 10-11-2006, 12:44 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by frankie_DJ
Hi,

I guess you guys didn't read my question properly. I know about separate compiling, header files and all that stuff. My question was:

How is it different putting a function declaration (NOT definition, just a declaration statement) inside of main, from putting it at the top of the source file (which can be through #include, if separate compilation, or explicitly)?
i think the main difference would be that the method that you are questioning is going to create hard to read code. the ways that have been mentioned above are the normal way to do it.. the fact that it is even allowed is strange to me, and from what i can tell the nested declaration does not even control the scope of the declaration in the expected way..

i would stay away from using that sort of declaration unless you can find a good reason for it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
warning: implicit declaration of function liguorir Linux - Software 5 10-22-2012 03:20 PM
C: Function declaration woes websinger Programming 4 08-08-2006 02:56 PM
Unintelligible declaration of signal function Nerox Programming 4 08-11-2004 04:45 PM
Arguments in PHP function declaration Parksy Programming 5 07-04-2004 04:22 PM
Problem with function declaration Linh Programming 3 04-26-2004 04:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration