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?


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.


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.


Do function submissions have to be reusable?

Default for Code Golf: Program, Function or Snippet?

  • \$\begingroup\$ 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? \$\endgroup\$
    – Doorknob
    Commented Jul 4, 2015 at 17:34
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2015 at 17:39
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Jul 4, 2015 at 17:51
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Jul 4, 2015 at 17:59
  • \$\begingroup\$ 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. \$\endgroup\$
    – BrainSteel
    Commented Jul 5, 2015 at 0:35
  • \$\begingroup\$ Also related. \$\endgroup\$ Commented Jan 26, 2016 at 13:03
  • \$\begingroup\$ 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.) \$\endgroup\$ Commented Jan 26, 2016 at 13:04
  • \$\begingroup\$ @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 ;-) \$\endgroup\$ Commented Jan 26, 2016 at 18:57

3 Answers 3


Yes, this is acceptable

Vote here if you think this is acceptable.

  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2015 at 19:44
  • 3
    \$\begingroup\$ We've always allowed defining helper functions outside the submitted function, and I think global variables fall in a similar category. \$\endgroup\$
    – isaacg
    Commented Jul 4, 2015 at 20:07
  • 2
    \$\begingroup\$ +1, provided that the function is still reusable. \$\endgroup\$
    – Geobits
    Commented Oct 14, 2015 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.

  • \$\begingroup\$ 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? \$\endgroup\$ Commented Jul 4, 2015 at 18:04
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2015 at 18:06
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2015 at 18:22
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2015 at 18:36

Not the answer you're looking for? Browse other questions tagged .