Default Arguments in functions and extern

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Default Arguments in functions and extern

Post by herby490 »

I'm making a simple game that will have multiple source files. I would like to have a default argument in one of my functions however if I declare it extern in another file and compile it I get an error saying that I am not allowed to reassign a value to it. If I remove the default value from the parameters in the extern file and compile it I get a different error saying that when I use it I have I am missing a parameter (I left it blank since the parameter is NULL).
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Default Arguments in functions and extern

Post by programmerinprogress »

Well, while I don't understand what exactly your problem is, I can offer a few tips when it comes to declaring prototypes in a separate file and setting default values etc.

- if you want to assign default values, ONLY assign once, preferably in the prototype, not the final definition
- if the answer is NULL, you still have to assign it if it's going to be a default argument, the compiler has to know that you intend to make it default, so it needs to be aware that you might "miss" the argument out
- default arguments start from right to left, you have to be consistent, you can't just say that you want your first argument to be default, and the rest to not be not

if follow these steps, then your function declarations should be valid, at least at single-file scope, i'll have to consult my big book to read up on the extern keyword, but from memory I understand its about allowing variables access to other files, right?, basically the opposite of the non-class use of the keyword static?
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Default Arguments in functions and extern

Post by hurstshifter »

programmerinprogress wrote:
- if you want to assign default values, ONLY assign once, preferably in the prototype, not the final definition
Agreed. I've ran into this problem in the past and it took me a while to figure it out (probably should have just asked). You probably had the extern initialized in the header and cpp file. Whenever I use an external I've trained myself to initialize it in the prototype, just to keep consistency with future projects.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Post Reply