Write a function
I propose this as the default for code golf questions that as for a function rather than a full program. I'm sorry if this all seems overly pedantic, but I've seen every single one of these points come up in actual questions and answers, and I'd like to have comprehensive rulings on them available.
I must admit I have a limited knowledge of languages, especially esoteric ones, so I welcome suggestions to make this more accessible to a wide class of languages.
Specification
The code must result in a named function (or analogous execution unit in your language) that fits the specifications.
The function, when called by name on its inputs, must evaluate to the desired outputs.
There can be code outside the function definition, whose characters are also counted.
Calling the function multiple times must produce the right answer each time.
Optional additional parameters are acceptable; mandatory additional parameters are not.
Explanations
The spec is meant to capture the idea that a function, once created, should be usable to transform inputs to outputs in the fashion asked for.
1) The code must result in a named function (or analogous execution unit in your language) that fits the specifications.
While in C-style languages, the function is usually created by a function definition, this allows for other methods such as a lambda-expression like f=lambda x:x*x
or a composition f=compose(g,h)
or a decoration f=memoize(f)
as long as the result is saved to a variable.
2) The function, when called by name on its inputs, must evaluate to the desired outputs.
This captures the requirement that the function be a function. Its inputs should be passed in, rather than taken through STDIN or taken from a global valuable. Its value must be returned, rather than saved to a variable or printed. To be clear, this does not mean an invocation of the function by included in the code, but that the function must behave as desired in invoked.
3) There can be code outside the function definition, whose characters are also counted.
This rule is an assurance that even if asked for a function, your code doesn't entirely have to do that function. Programs might make imports, saved global variable, and define auxiliary functions outside that function.
4) Calling the function multiple times must produce the right answer each time.
In other words, it's not acceptable to have your function only work the first time. Subsequent calls within a single run should also work according to the spec. This could come up if the function modifies a global value.
This does not preclude the function modifying the value it was passed in. So, the function f
should print the same output twice on print f(1,2); print f(1,2)
but not necessarily l=[1,2]; print f(l); print f(l)
.
Here's an answer where this question can up in practice, with Falko's suggestion to save a character by defining the initial list as an optional parameter def f(a,b=[0]*1000):
causes it to be overwritten for subsequent runs.
5) Optional additional parameters are acceptable; mandatory additional parameters are not.
In other words, if asked for a function that takes two numbers, it's fine to have one that takes two numbers and an optional third number, as long as it behaves as asked when given only two numbers. The reasoning is that this would pass any test according to the specs, so it can behave arbitrarily in the non-spec case of three numbers. However, having a mandatory additional parameter and saying "call f
as f(a,b,0)
does not fit the spec.
An optional additional parameter might be useful for golf to define a recursive function or to save a method name to a variable before the function body.
Things not addressed
Input/outputs formats: If asked for a function that takes no numbers, can yours instead take a list/array/tuple of two numbers? Can you output a float if asked for a whole number?
Fallbacks: What should a language that does have functions do? I think this should be addressed with a general convention of "Do the closest you can, but only if what was required was impossible in the language.