LinuxQuestions.org
Visit Jeremy's Blog.
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 07-31-2005, 04:52 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
function placement / prototyping


Here is a really silly question im sure but its becomming confusing.


what is the ansi standard for function prototyping and placement.

is it like this

Code:
 
int func_name(void); 

int main () 
{ 
   code code code 
   func_name(); 
return 0; 
} 

int func_name(void)
{
   funcode 
}

or like this

Code:
 
 

int main () 
{ 
   code code code 
   func_name(); 
return 0; 
} 

int func_name(void)
{
   funcode 
}
or even this way!

Code:
 
int func_name(void)
{
   funcode 
}

int main () 
{ 
   code code code 
   func_name(); 
return 0; 
}
sorry about the subject i relized i named it wrong after i posted

Last edited by exvor; 07-31-2005 at 04:59 PM.
 
Old 07-31-2005, 05:17 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
The first case you presented is the one I generally see used, and makes the most sense. The allows you to move the function prototype (aka, function declaration) outside the source file into a header file to share the function with other programs. It is also the only version that doesn't cause warnings to be generated, IIRC. The second version is the WORST form of all, because it causes the compiler to "guess" about the definition for the function.
 
Old 07-31-2005, 05:30 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Cool thats what ive been doing is the first one.


i havent seen the second one anywhere

however the reson for my question was really because of the first and thired ones.

silly gnu programming tutorial it talkes about the ansi standard says the rest of the
examples in that section are the ansi standard (with prototypes) .

but then when you look at them there like the 3rd one not the first

lol go figure
 
Old 07-31-2005, 06:23 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I've seen the third method as a silly shortcut for throwaway programs (cases where it was developed for a single use). IMHO, it makes code readability and maintainability a bear. Also, what about the following case:
Code:
void handler1(void *x){
  if(x->needs_handler2)
    handler2(x);
}

void handler2(void *x){
  if(x->needs_handler1)
    handler1(x);
}

int main(int argc,char **argv){
  ...
}
Really a quite silly example, but such "back and forth" constructs do exist and are essentially impossible to do properly with the 3rd method.
 
Old 07-31-2005, 08:06 PM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
regarding topologically unsortable (i.e. cyclic) function prototype dependencies: why not use the fourth method?

Code:
void f1(void* x) {
    void f2(void*);
    f2(x);
}

/* similarly for f2 */
In that way, the functions that need to know about f2 do so, the rest doesn't.

For reference, K&R do it in their book (2nd ed., at least).

hth --Jonas
 
Old 07-31-2005, 10:14 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I always hear people refer to K&R style, but I've always found it ugly. Aren't they the ones that reccomend:
Code:
void myfunc()
{
    return;
}
I can't stand that opening-bracket-on-it's-own-line style. Just a personal thing, though.

I also think the "fourth style" above has one big flaw... it becomes a PAIN for mantainability. Every time you change f2(), you have to change that line in EACH function.
 
Old 08-01-2005, 02:53 PM   #7
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
... PAIN for maintainability. Every time you change f2, you have to change ... EACH ...
no.

Code:
#define F2_PROTOTYPE void f2(void*)

void f1(void* x) {
    F2_PROTOTYPE;
    f2(x);
}
 
Old 08-01-2005, 02:55 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
ok, fine. Is there any GOOD reason to do that?
 
Old 08-01-2005, 03:34 PM   #9
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
Originally posted by Matir
ok, fine. Is there any GOOD reason to do that?
no.
 
Old 08-01-2005, 03:42 PM   #10
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Quote:
Originally posted by jonaskoelker
no.
Ok, I didn't think so, but I just thought I'd check.
 
Old 08-01-2005, 03:50 PM   #11
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Yes actually I have seen it in the form

Code:
 

int function(char * ); 

int function (char *stupid)
{
   //somedata 
} 


int main () 
{ 
     int something; 
     char pointer[];

     something  = function (pointer); 
     
     return 0 ; 
}
but i wasent sure what the signficance of this was.

but the original question was what is the ANSI standard way for best so called
portablity.
 
Old 08-01-2005, 03:59 PM   #12
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
The variable NAME in the function declaration/prototype is optional. I usually include it as I try to make my names informative.

What do people usually find as style for documenting function prototypes? Put the comment above or below the prototype?

That's always confused me.
 
  


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
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Is the wait function is the same as the sleep function ? Linh Programming 3 04-28-2004 12:39 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

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

All times are GMT -5. The time now is 10:52 PM.

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