ROBOTC.net forums
http://robotc.net/forums/

Default values in forward declaration
http://robotc.net/forums/viewtopic.php?f=1&t=6399
Page 1 of 1

Author:  Ernest3.14 [ Sun Jul 28, 2013 3:41 pm ]
Post subject:  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? :?

Author:  Ernest3.14 [ Sun Jul 28, 2013 8:19 pm ]
Post subject:  Re: Default values in forward declaration

Is it good practice to ask about best practices?

Author:  mightor [ Sun Jul 28, 2013 11:53 pm ]
Post subject:  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

Author:  Ernest3.14 [ Mon Jul 29, 2013 3:11 pm ]
Post subject:  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`?

Author:  mightor [ Tue Jul 30, 2013 10:54 am ]
Post subject:  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

Author:  Ernest3.14 [ Tue Jul 30, 2013 3:30 pm ]
Post subject:  Re: Default values in forward declaration

Ok. Thanks! Should I have the value in comments next to the implementation? Or is that unnecessary?

Author:  mightor [ Wed Jul 31, 2013 1:02 am ]
Post subject:  Re: Default values in forward declaration

Only if you think they can't read the code :)

= Xander

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/