LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   function headers (https://www.linuxquestions.org/questions/programming-9/function-headers-348657/)

exvor 07-31-2005 04:52 PM

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

Matir 07-31-2005 05:17 PM

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.

exvor 07-31-2005 05:30 PM

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 :p

Matir 07-31-2005 06:23 PM

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.

jonaskoelker 07-31-2005 08:06 PM

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

Matir 07-31-2005 10:14 PM

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.

jonaskoelker 08-01-2005 02:53 PM

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);
}


Matir 08-01-2005 02:55 PM

ok, fine. Is there any GOOD reason to do that?

jonaskoelker 08-01-2005 03:34 PM

Quote:

Originally posted by Matir
ok, fine. Is there any GOOD reason to do that?
no.

Matir 08-01-2005 03:42 PM

Quote:

Originally posted by jonaskoelker
no.
Ok, I didn't think so, but I just thought I'd check. :)

exvor 08-01-2005 03:50 PM

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.

Matir 08-01-2005 03:59 PM

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.


All times are GMT -5. The time now is 08:47 AM.