LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-12-2009, 08:57 PM   #1
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Rep: Reputation: 16
Question function prototype


Is it ok to omit the parameter's names on functions prototypes? I know it will give you more information about what the function expects exactly, but I always put a comment box on top of my functions explaining what my parameters are gonna be used for.

whats the deal?
 
Old 04-12-2009, 10:28 PM   #2
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
You can omit the parameter's name on a function prototype, however I would say best practice would be to include it.

You should code in a style that makes it easy for others to see what is going on, easy for you to see what you have written and to prevent mistakes.

One thing about commenting - what if you update the function prototype but forget to update the comment? You will then have unclear code with a contradictory comment. Including the parameter name prevents this.

--Ian
 
Old 04-12-2009, 11:37 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by IBall View Post
One thing about commenting - what if you update the function prototype but forget to update the comment? You will then have unclear code with a contradictory comment. Including the parameter name prevents this.
And what if you update the function definition in a way that changes the name and meaning (but not the type) of one of the parameters, but forget to update the prototype? You will then have unclear code with a prototype which is contradictory for humans, though not for the compiler.

Unlikely, but realize that the parameter name in a prototype is just another comment.
 
Old 04-13-2009, 12:19 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I actually have to deal with legacy code where the programmer felt, for whatever reason, he didn't need names on his prototypes (and sometimes even his implementation classes: no kidding!)

Syntactically: permissible.

In practice: it's Evil. Don't do it!

IMHO .. PSM
 
Old 04-13-2009, 07:04 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
There are situations where it can be useful not to include the parameter names. (Whether "useful" is the right word I'm not sure) I have some functions that are used just as callback functions but they do not always require the parameter that will be passed in, for those functions I omit the parameter name in the function definition, thus avoiding warning from the compiler but I still tend to keep them in the declaration, for clarity, but omitting it would indicate (to me) that they were not needed by the function.
 
Old 04-13-2009, 09:58 AM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by graemef View Post
they do not always require the parameter that will be passed in, for those functions I omit the parameter name in the function definition, thus avoiding warning from the compiler
Just for the record:
  1. they're errors, not warnings; and
  2. one avoids these errors not by omitting parameter names, but including them.
To see this in action, run the following bash script:
Code:
#!/bin/bash

gcc --version | head -1;     \
cat > 1.c <<EOD;             \
cat -n 1.c | sed 's/^    //';\
gcc -Wall 1.c -o 1;          \
if [ $? = 0 ] ; then ./1; fi
#include <stdio.h>

void name_in_no_places  (int      );
void name_in_declaration(int param);
void name_in_definition (int      );
void name_in_both_places(int param);

int main(void)
{
  name_in_no_places  (1);
  name_in_declaration(2);
  name_in_definition (3);
  name_in_both_places(4);

  return 0;

} /* main() */

void name_in_no_places  (int      ){printf("name_in_no_places  ()\n");}
void name_in_declaration(int      ){printf("name_in_declaration()\n");}
void name_in_definition (int kazam){printf("name_in_definition ()\n");}
void name_in_both_places(int kazam){printf("name_in_both_places()\n");}
EOD
When I did so, I got this output:
Code:
gcc (GCC) 4.2.3
 1	#include <stdio.h>
 2	
 3	void name_in_no_places  (int      );
 4	void name_in_declaration(int param);
 5	void name_in_definition (int      );
 6	void name_in_both_places(int param);
 7	
 8	int main(void)
 9	{
10	  name_in_no_places  (1);
11	  name_in_declaration(2);
12	  name_in_definition (3);
13	  name_in_both_places(4);
14	
15	  return 0;
16	
17	} /* main() */
18	
19	void name_in_no_places  (int      ){printf("name_in_no_places  ()\n");}
20	void name_in_declaration(int      ){printf("name_in_declaration()\n");}
21	void name_in_definition (int kazam){printf("name_in_definition ()\n");}
22	void name_in_both_places(int kazam){printf("name_in_both_places()\n");}
1.c: In function 'name_in_no_places':
1.c:19: error: parameter name omitted
1.c: In function 'name_in_declaration':
1.c:20: error: parameter name omitted
 
Old 04-13-2009, 05:05 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
For the record
Code:
#include <cstdio>

void name_in_no_places  (int      );
void name_in_declaration(int param);
void name_in_definition (int      );
void name_in_both_places(int param);

int main(void)
{
  name_in_no_places  (1);
  name_in_declaration(2);
  name_in_definition (3);
  name_in_both_places(4);

  return 0;

} /* main() */

void name_in_no_places  (int      ){printf("name_in_no_places  ()\n");}
void name_in_declaration(int      ){printf("name_in_declaration()\n");}
void name_in_definition (int kazam){printf("name_in_definition ()\n");}
void name_in_both_places(int kazam){printf("name_in_both_places()\n");}
I get the following output
Code:
$ g++ test.cpp -Wextra -Wall -o test
test.cpp:21: warning: unused parameter ‘kazam’
test.cpp:22: warning: unused parameter ‘kazam’
Just one more language difference between C and C++
 
Old 04-14-2009, 12:57 AM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Ooo, good point!
 
Old 04-14-2009, 01:10 AM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
charlitos -

Anonymous parameters: Just say "No."

You'll earn a tremendous debt of gratitude from any developers who'll work on your code afterwards ;-)

IMHO .. PSM

PS:
I believe in comments, and I believe that "comment boxes" have their place...

... but I also believe the aphorism "Comments are Lies".

"Self-documenting code" beats comments. And named parameters are arguably a good example of self-documenting code...

Last edited by paulsm4; 04-14-2009 at 01:11 AM.
 
Old 04-14-2009, 07:14 AM   #10
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by paulsm4 View Post
"Self-documenting code" beats comments.
Absolutely.
Quote:
Originally Posted by paulsm4 View Post
named parameters are arguably a good example of self-documenting code...
Correct. But seen another way, named parameters in function prototypes (function declarations) are arguably comments.
 
  


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
How many prototype of function main in c? ypzhuang Programming 9 12-13-2008 07:53 AM
no previous prototype Steve Riley Linux From Scratch 2 08-11-2006 11:54 AM
Why 'extern' on function prototype Hko Programming 11 02-08-2006 04:50 PM
specifying a prototype dhanakom Programming 2 09-02-2003 03:07 AM
Assembly for Rapid Prototype mikeshn Programming 1 03-09-2002 04:18 PM

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

All times are GMT -5. The time now is 07:30 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