View unanswered posts | View active topics It is currently Tue Jul 29, 2014 6:59 pm






Reply to topic  [ 7 posts ] 
Default values in forward declaration 
Author Message
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Default values in forward declaration
I think this may apply to C in general, but am I allowed to have default values in both the forward declaration and implementation? Because my code compiles if I only declare default values once. My question is, which is best practice? Should I have default values in my header file (forward declaration), or in my implementation (.c file)? And should I specify the default value with comments? Or would that be difficult to maintain? :?

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Sun Jul 28, 2013 3:41 pm
Profile
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Re: Default values in forward declaration
Is it good practice to ask about best practices?

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Sun Jul 28, 2013 8:19 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: Default values in forward declaration
Only if you practice first...

In my code I tend to either assign a value (usually 0) to normal types or use memset() to 'zero out' array types. I don't like leaving variables unassigned.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jul 28, 2013 11:53 pm
Profile WWW
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Re: Default values in forward declaration
mightor wrote:
Only if you practice first...

Sorry, now I see how that was a really bad question... :)
What I meant was, default values for functions. For example,
Code:
void foo(int x=15);
//some code
void foo(int x=15) {
    x++;
}

Currently that would be illegal. Where should I put the `x=15`?

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Mon Jul 29, 2013 3:11 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: Default values in forward declaration
You put it in the prototype only. It makes the argument optional as well. I use this in my suite, quite a bit.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jul 30, 2013 10:54 am
Profile WWW
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Re: Default values in forward declaration
Ok. Thanks! Should I have the value in comments next to the implementation? Or is that unnecessary?

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Tue Jul 30, 2013 3:30 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: Default values in forward declaration
Only if you think they can't read the code :)

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Wed Jul 31, 2013 1:02 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.