When submitting a function, can global variables be declared outside the function? [duplicate]

1. When submitting a function, can global variables be declared outside the function?

For example, if I have the C code

f(n){int a,b,c=4;dosomething;}


am I allowed to save 4 bytes as follows?

a,b,c=4;f(n){dosomething;}


I had always assumed not, but answers on Print an ascii spiral in O(log n) memory have led me to wonder.

2. EDIT: as an aside, what about this? I'm pretty sure this one is totally unacceptable, but if you think it is acceptable, please write a corresponding answer.

f(n,a,b,c){dosomething;}


Where only parameter n is actually useful for passing information to the function and the parameters passed to a,b,c can either take arbitrary values or the answerer specifies "the function must be called with c=4."

Reversal of edit: As pointed out by Doorknob there are examples of this being considered accepable if the function has additional arguments but may be called with only the arguments specified in the question.

Relevant:

Do function submissions have to be reusable?

Default for Code Golf: Program, Function or Snippet?

• I have seen both using extra arguments as undefined (ex. function f(n,a,b,c){...}; f(100) instead of function f(n){var a,b,c;...}; f(100)) and default arguments (ex. def f(n,a=1,b=2,c=3):...; f(100) instead of def f(n):a=1;b=2;c=3;...; f(100)) being widely accepted as okay. You might want to clarify point 2: does this only apply to when the function actually has to be called with more arguments than specified?
– Doorknob Mod
Jul 4 '15 at 17:34
• I've seen extra function arguments used a lot, so I'm fairly sure that it's generally accepted. Since C does not check function arguments, you can still call it with a single argument, so it doesn't change how the function needs to be called. Jul 4 '15 at 17:39
• @doorknob thanks, text striked through and answers (manually) rolled back to the original version. I couldn't work out how to do an auto rollback. Jul 4 '15 at 17:51
• @RetoKoradi the extra function arguments trick in C is an ugly one, but I have seen it before. I never checked to see if calling a functon with some arguments missing actually works till today. As the function can still be called as f(n) I guess it's OK, but it makes this meta post somewhat irrelevant for C as the length then becomes identical. Jul 4 '15 at 17:59
• As someone who writes C answers frequently (almost exclusively), I had a chat with Martin Büttner a few moons ago, and if I remember correctly, the consensus was that this was made unacceptable by this default. In this example, if the value of c ever deviates from 4, f becomes non-reusable. Jul 5 '15 at 0:35
• Also related. Jan 26 '16 at 13:03
• Actually I think this sort of supersedes your question, doesn't it? Would you object to closing this as a duplicate? (I'm happy linking back to this, to support my argument there with the consensus on this question.) Jan 26 '16 at 13:04
• @MartinBüttner This must have come on your radar because of Qwerty's answer today (I was surprised to get another answer.) Yes I see the argument for closing this and cross linking the two questions. You're the boss Martin, do as you think best ;-) Jan 26 '16 at 18:57

Yes, this is acceptable

Vote here if you think this is acceptable.

• The only argument against that I can see is that it makes the functions not thread-safe, but any function which prints to stdout isn't thread-safe, so that argument carries no weight. Jul 4 '15 at 19:44
• We've always allowed defining helper functions outside the submitted function, and I think global variables fall in a similar category. Jul 4 '15 at 20:07
• +1, provided that the function is still reusable. Oct 14 '15 at 15:38

If it is forbidden, it have to be forbiden explicitly.

Some languages (like javascript) allow implicit global variable declaration inside the function. As I think there is a little difference between these cases, so declaring variables outside of the function should be allowed too.

By the way, using of helper functions is allowed too.

If the author wants to prevent global variable, he have to write in challenge rules that function have to be free of side effects. I've seen some challenges with this requirement.

No, this is not acceptable

Vote here if you think this is not acceptable.

• Functions using global variables is a completely standard language feature in C. I don't see a reason why it should not be allowed. I think it would set a dangerous precedence once you start restricting the language features that can be used. Are we going to maintain a list of features of each language that are not permitted? Jul 4 '15 at 18:04
• Related thought: Some languages don't have local variables. If they can't use global variables, they could never use variables at all. At least not if they want to submit solutions as functions. Jul 4 '15 at 18:06
• It's an interesting point. It's the reason why I have tended to submit C code requiring a lot of intitialization to zero (particularly for arrays) as a full program. Then there is the issue than when an array must be passed as a parameter, it must be done as a function parameter pointer to a global array, because C does not support passing arrays as parameters. But that's a separate issue. Jul 4 '15 at 18:22
• If the function has to be reusable (looks like that's the case based on the related question you linked), relying on the zero init is not really valid anymore. Jul 4 '15 at 18:36