Topic: Formal parameter names are not required in function definitions. Why?!


Author: jackklein@spamcop.net (Jack Klein)
Date: Sat, 27 Nov 2004 23:54:54 GMT
Raw View
On Thu, 25 Nov 2004 04:47:34 GMT, eac203@ecs.soton.ac.uk (Edward A
Cullen) wrote in comp.std.c++:

> This is valid C++ code, but invalid C code.
>
> ***
>
> int f(char*, int);
>
> int f(char*, int x) // note char* is not named.
> {
>  return x;
> }
>
> ***
>
> C/C++ allows declarations without parameter names, but C requires that
  ^^^^^

There is no such thing as "C/C++", and your entire post is about the
point that there is a difference between C and C++ in this regard.

> parameters of definitions are named. Why does C++ not have this
> behaviour? What value is added by allowing formal parameters in
> definitions not to be named?

The C++ language allows names to be omitted for parameters that are
not referenced in the body of the function.  This in turn allows the
optional warning functions of compilers, and of lint-like tools, to
ignore them and not issue messages about "unused parameters".

In C, one must use things like compiler-specific pragmas, or silly
lines like:

  x = x;

..or:

  (void)x;

..if one's tools can't otherwise be told to ignore these, and one's
requirements are to remove all such warnings.

> I initially thought that this was a hangover from C, where concievably,
> one might be doing inline assembly and therefore did not need the names
> of variables, only that one knows they are on the stack. However, having
> found that C does not support this, it made me wonder...
>
> Personally, I can see no value in allowing this syntax - even if a
> parameter is not used, what is the harm in giving it a name?
>
> This quirk initially came to light while I was playing with
> constructors, an excerpt of which follows:
>
> ***
>
> class Animal
> {
> public:
>  Animal(char*, int);
> private:
>  char* name;
>  bool hungry;
> }
>
> Animal::Animal(char*, bool isHungry): // Again, note char* is not named.
>  name(name),
>  hungry(isHungry)
> {
>  // Does some stuff here.
> }
>
> ***
>
> The error is subtle; because of the behaviour outlined above, the
> compiler (tested with G++ 3.2.3 and VS7.1) does not throw an error, as
> Animal::name is defined and thus re-defined in terms of itself
> (producing a runtime error that was VERY difficult to trace).
>
> Of course, I have learnt that one must use different names for formal
> parameters and locals/members, in order to catch this kind of error at
> compile time... But why should I, when, surely, the copiler/language
> should provide this basic sanity checking?

There is arguably more need for this in C++ than C, with functions
from derived classes overriding those of base classes.

Consider a base class for simple geometric shapes such as circle,
square, triangle, rectangle.  Such a class might define two members to
contain dimensions, and functions which take two dimension arguments,
because the rectangle needs (width,height) and the triangle requires
(base,height).  But circles and squares need only one dimension, and
their member functions would ignore the second parameter.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Thu, 25 Nov 2004 08:01:53 GMT
Raw View
In article <41a49048@news.ecs.soton.ac.uk>, Edward A Cullen
<eac203@ecs.soton.ac.uk> writes
>C/C++ allows declarations without parameter names, but C requires that
>parameters of definitions are named. Why does C++ not have this
>behaviour? What value is added by allowing formal parameters in
>definitions not to be named?
>
>I initially thought that this was a hangover from C, where concievably,
>one might be doing inline assembly and therefore did not need the names
>of variables, only that one knows they are on the stack. However,
>having found that C does not support this, it made me wonder...
>
>Personally, I can see no value in allowing this syntax - even if a
>parameter is not used, what is the harm in giving it a name?

I find it useful in that it does exactly what it promises, it turns off
any diagnostics that would otherwise occur if a parameter is unused in
the body of the function.

You maybe surprised that there are any such cases but they actually
occur quite often. For example I have a general line drawing function
whose last parameter is a function that determines exactly what is done
at each 'point' of the line. All these 'policy' functions must have
exactly the same type, yet only some of them will actually provide a
plotting color.

I make that the final parameter. In plotting policy functions where a
color is meaningless I provide a default argument in the declaration and
an unnamed parameter in the definition. Everyone is happy and I do not
get spurious warnings.

The interesting thing, to me, about the above was that it is also an
example of real value being provided by default arguments (normally
defaults are better provided by overloading as that solution does not
have the same restriction as apply to default arguments)


BTW, your problem with self-initialisation is best tackled by using good
tools that warn you of such. I am pretty sure that PCLint++ does
(because I raised the issue with GimpelSoft when I first became aware of
self-initialisation over a decade ago)


--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: technews@kangaroologic.com ("Jonathan Turkanis")
Date: Thu, 25 Nov 2004 17:27:59 GMT
Raw View
"Edward A Cullen" <eac203@ecs.soton.ac.uk> wrote:

> C/C++ allows declarations without parameter names, but C requires that
> parameters of definitions are named. Why does C++ not have this
> behaviour? What value is added by allowing formal parameters in
> definitions not to be named?

Requiring dummy parameters to have names would just make more work for those
reading and writing code. Consider

   struct bigint {
       bigint operator++(int charles);
       ...
   };

Jonathan


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]