Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which function header do you prefer?

Reply
Thread Tools

Which function header do you prefer?

 
 
istillshine@gmail.com
Guest
Posts: n/a
 
      04-04-2008
int foo(x, y, z)
double *x;
int y;
double z;
{
/* do something */
}

or

int foo(double *x, int y, double z)
{
/* do something */
}


I notice the first one appeared in 1978's The C Programming Language
book.
The second edition of the book adopted the second one. The first one
seem more
readable when there are many arguments.
 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      04-04-2008
writes:
> int foo(x, y, z)
> double *x;
> int y;
> double z;
> {
> /* do something */
> }
>
> or
>
> int foo(double *x, int y, double z)
> {
> /* do something */
> }
>
>
> I notice the first one appeared in 1978's The C Programming Language
> book.


The 2nd form is called a prototype, and makes the compiler do type
checking on the arguments and convert them to the types of the parameter
if needed. E.g.
int foo(x) long x; {...}
... foo(3); ...
is wrong, it should have been foo(3L). But foo(3) is correct if
you used a prototype: int foo(long x);'.

> The second edition of the book adopted the second one. The first one
> seem more readable when there are many arguments.


Then write the 2nd like this:
int foo(
double *x,
int y,
double z)

--
Hallvard
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-04-2008
wrote:
> int foo(x, y, z)
> double *x;
> int y;
> double z;
> {
> /* do something */
> }
>
> or
>
> int foo(double *x, int y, double z)
> {
> /* do something */
> }
>
>
> I notice the first one appeared in 1978's The C Programming Language
> book.
> The second edition of the book adopted the second one. The first one
> seem more
> readable when there are many arguments.


There are more issues than readability.
The first form, by not specifying the types in the parameter list,
applies standard rules of promotion of types but does not lead to
checking that the types match. The second allows the compiler to check
that the values of the types of the arguments in an invocation of the
function are convertible to the the types of the parameters the function
expects, and if they
a) are not convertible to issue a diagnostic, and
b) are convertible but not identical to apply the appropriate conversion.

Suppose you had the simple function
double square(x)
double x;
{
return x*x;
}
and called it with
square(1);
The compiler will generate a call with an _int_ of value 1, which
may not bear any semblance to a _double_ with value 1.

But suppose you had the simple function
double square(double x)
{
return x*x;
}
and called it with
square(1);
The compiler can now convert value 1 to a double and pass that to the
function.

You might ask why you can't do that with the first kind of function
definition. You could, it you were building a language from the ground
up, but the rules for the first way of handling argument lists were
established before the second way existed.

As for readability, suppose you use whitespace to rewrite the second as
int foo(double *x,
int y,
double z)
{ /* .... */ }

The whitespace does nothing to the meaning, but it now has all the
advantages you might see in
int foo(x, y, z)
double *x;
int y;
double z;
{ /* .... */ }
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2011: Which Ruby books have you read? And which would you recommend? Aston J. Ruby 13 03-16-2011 07:36 AM
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
How do you find the header file you need? Jim Holder C Programming 5 12-21-2003 06:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57