85
\$\begingroup\$

It looks like we have a consensus that we want certain defaults for the format which answers are expected in for . On that poll, the question arose twice, which input/output formats should be allowed for programs and functions.

So here is another poll. This one works different though. All the input/output methods are independent of each other, so there will be one answer per method. Upvote all you think are reasonable for the default. Downvote those which you think shouldn't be allowed unless the OP explicitly permits them.

To keep this remotely manageable, I have not posted individual answers for all possible inputs for functions. So there are only four: functions can take input/output via their arguments and return values. Or functions can use any method full programs can. I don't think there is any point in (say) allowing programs to take input from STDIN (only) and to allow functions to take input from ARGV (only) or something like that. If you disagree, please leave a comment.

If I've overlooked an I/O method, feel free to add your own answer.

Note: Some votes have been reverted because they were detected as serial voting. If you vote on multiple answers, please leave some time between votes.

A method is allowed if it has 5 net votes and at least twice as many upvotes as downvotes.

Update

The current results of the polls are now part of the tag wiki. Please notify me, if results change significantly and the wiki should be amended.

\$\endgroup\$
  • \$\begingroup\$ Related (Can numeric input/output be in the form of byte values) \$\endgroup\$ – mbomb007 Dec 13 '16 at 16:10
  • 1
    \$\begingroup\$ "The current results of the polls are now part of the tag wiki" which tag wiki? meta wiki has nothing for code-golf tag. ppcg wiki has codegolf.stackexchange.com/tags/code-golf/info but it's missing a lot of answers here. \$\endgroup\$ – Sparr Jul 18 '18 at 18:17

76 Answers 76

1
\$\begingroup\$

Functions may output via throwing the output value as an exception, if it could be caught, and read by the code catching the exception

\$\endgroup\$
1
\$\begingroup\$

Functions may generate different output via different exceptions if they could be caught using different code

For example, in C++, you could use throw 1; and throw true; to output two distinct values. They could be caught by catch(int) and catch(bool) in a try block respectively. (In most other languages, you could only throw values in an exception class.)

\$\endgroup\$
1
\$\begingroup\$

Functions may take input from the output of a callback function

The callback function should be provided using a valid input method.

Complementing

\$\endgroup\$
0
\$\begingroup\$

black-box functions to be taken as input can be assumed to be predefined under a given name only if they cannot be passed as an argument.

This holds for languages that do have classical functions, but cannot pass around functions like other values. This holds for instance for: Java up to version 7

This proposition is the opposite of that one which does not have this restriction.

\$\endgroup\$
  • 2
    \$\begingroup\$ -1 because I don't think this is well-defined (e.g. are functions in Underload passed as an argument? What about Python's functions, which aren't black box because you can inspect their bytecode?) \$\endgroup\$ – Esolanging Fruit Nov 26 '17 at 23:39
  • 1
    \$\begingroup\$ Also, Don't allow / ask for different things in languages with different capabilities (related issue). \$\endgroup\$ – Esolanging Fruit Nov 26 '17 at 23:45
  • 1
    \$\begingroup\$ @EsolangingFruit That doesn't seem to be the case here, it's not the challenge asking for different stuff for different languages, it's more like allowing more languages to compete by using more lenient consensuses for them, since otherwise they couldn't compete at all. \$\endgroup\$ – Erik the Outgolfer Dec 10 '17 at 12:14
  • 2
    \$\begingroup\$ @EriktheOutgolfer Fair, but my view is that, to avoid ambiguity and rule-lawyering, we should allow all languages to do this. \$\endgroup\$ – Esolanging Fruit Dec 10 '17 at 20:57
0
\$\begingroup\$

If a program is supposed to take a whole number as input, it may accept the number as a unary string.

\$\endgroup\$
-2
\$\begingroup\$

Programs may use the camera and microphone

I wouldn't expect to see this very often, but it could be applicable to my upcoming challenge.

\$\endgroup\$
  • 2
    \$\begingroup\$ This has my vote \$\endgroup\$ – TheDoctor Nov 4 '14 at 23:40
  • 51
    \$\begingroup\$ I don't think this should be a default. Definitely fine if asker specifies it as a valid input format for the question! Excited to see your upcoming challenge involving this. \$\endgroup\$ – chilemagic Nov 5 '14 at 2:12
-3
\$\begingroup\$

Source files or libraries may output via the return value importing them

For example, this PHP code has no effects as a complete program:

<?php return 'Hello, world!';

But as a library named '1.php', the return value could be printed to stdout using this:

<?php echo include '1.php';
\$\endgroup\$
-3
\$\begingroup\$

eval is considered an interpreter and the eval'd code may output via the return value

For example, this PHP code has no effects as a complete program in the php interpreter:

return 'Hello, world!';

But the return value is accessible using the eval interpreter:

<?php echo eval("return 'Hello, world!';");
\$\endgroup\$
  • \$\begingroup\$ Just for clarification, this only applies in challenges that allow snippets, correct? I don't think you're suggesting we allow snippets by default, but just to be safe... \$\endgroup\$ – ETHproductions Feb 11 '17 at 2:22
  • \$\begingroup\$ @ETHproductions Rewrote to avoid mentioning that. \$\endgroup\$ – jimmy23013 Feb 12 '17 at 2:41
-3
\$\begingroup\$

Functions may output by setting the default variable(s)

Perl has a "default" scalar $_. Unless specifically overridden by the =~ operator, regular expressions and such will operate on this variable. It also has @_, the "default" array, and %_, the "default" hash.

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't see how this is any different than hardcoding I/O with variables, which is not allowed. \$\endgroup\$ – Mego Dec 5 '17 at 20:35
  • \$\begingroup\$ @Mego What do you mean? I'm not entirely sure what "hardcoding I/O with variables" refers to. \$\endgroup\$ – Stephen Leppik Dec 5 '17 at 21:21
  • \$\begingroup\$ codegolf.meta.stackexchange.com/a/2423/45941 \$\endgroup\$ – Mego Dec 5 '17 at 21:23
  • \$\begingroup\$ Ah, I see then. \$\endgroup\$ – Stephen Leppik Dec 5 '17 at 21:26
  • \$\begingroup\$ This is basically Ans in TI-Basic, and isn't too different from the top of the stack in stack based languages. Following functions if defined in the right way don't need to specify the variable name to use it. It's only technically a variable, but don't need to be used like a variable. I don't see good reasons to disallow it. \$\endgroup\$ – jimmy23013 May 28 at 20:49
-4
\$\begingroup\$

Functions may get input from predefined variables in languages where this is standard

In some BASIC dialects, the standard way to implement functions is to use GOSUB (similar to CALL in assembly). You can't pass variables to GOSUB, so function arguments/returns are just global variables. If you need a function that takes a number as input, and outputs that number multiplied by 2, the normal way to do this would be something like:

@TIMES2
 OUT=IN*2
RETURN

To call it, you would do

IN=7
GOSUB @TIMES2
'result is in variable OUT

The exact same process is allowed in assembly languages.

\$\endgroup\$
  • 2
    \$\begingroup\$ I don't think it's necessary to allow things that are "Kind of like functions" just because a language doesn't have functions. As long as an answer can be written as a full program using existing allowed I/O I don't think we need a special exception to allow answers in "function-like behavior". \$\endgroup\$ – Kamil Drakari Oct 27 '17 at 19:26
  • \$\begingroup\$ Then why do we allow the same thing in "assembly" languages? \$\endgroup\$ – 12Me21 Oct 27 '17 at 19:53
  • 3
    \$\begingroup\$ Answers talking about "Assembly" refer to any language which does not have an output other than storing things in a special register. If a program can be written in the language which inputs and outputs in a more standard way then it doesn't need those allowances. \$\endgroup\$ – Kamil Drakari Oct 27 '17 at 20:07
-5
\$\begingroup\$

A full program may be input or output any time a function could

If we can do this:

x=>y=>x+y

Then we can do this:

print("print(int(input())+"+input()+")")
\$\endgroup\$
  • 9
    \$\begingroup\$ That Python code is...a curried program. what. \$\endgroup\$ – CalculatorFeline Jun 20 '17 at 2:18
  • \$\begingroup\$ +1 from me. If functions can return other functions, full programs should be able to print other full programs \$\endgroup\$ – MilkyWay90 Jun 16 at 18:25
-8
\$\begingroup\$

Languages that cannot obtain world-state-sensitive information can accept the information as an additional input

Some challenges may require the use of the current time, cryptographic random values, or other information present on the system used to run the program. Additionally, many languages (for various reasons) lack the ability to access this information, and thus compete at all.

To overcome this, answers may accept such information as additional input in a minimally processed format (eg: unix timestamp instead of a date string, a single random value instead of a random generator function).

\$\endgroup\$
  • \$\begingroup\$ Well, cryptographic random values can still be generated in Turing tarpits. \$\endgroup\$ – Stephen Leppik Dec 5 '17 at 18:29
  • 1
    \$\begingroup\$ @StephenLeppik I was thinking more of completely deterministic languages where you would need the random seed for any subsequent random values. \$\endgroup\$ – Οurous Dec 5 '17 at 18:49
  • \$\begingroup\$ I don't like this idea (it's related to this meta. Accessing system information can require quite some bytes (maybe an import and/or system calls with long function names). Taking just another input variable instead is an unfair advantage. We always had challenges which cannot be solved with all languages (e.g. graphical output), but most can, so it's not a problem. \$\endgroup\$ – nimi Dec 6 '17 at 23:03
  • 2
    \$\begingroup\$ @nimi This isn't for those languages that can access system information/resources only in an expensive manner. This proposal is for languages (like brainfuck) that have no way to access that information/those resources. Because BF has no source of randomness, this proposal would allow BF to take a seed value as an additional input to be fed into a PRNG. \$\endgroup\$ – Mego Dec 12 '17 at 6:19
  • \$\begingroup\$ @Mego: yes, I know, maybe I wasn't clear enough. My point is: getting some system information via additional input is usually much cheaper (in bytes) than having to call some system library functions. Some languages would be allowed to use the former, others not, although both compete within the same challenge. That's unfair. ... \$\endgroup\$ – nimi Dec 12 '17 at 14:36
  • 2
    \$\begingroup\$ @Mego: (cont.) Stupid example to illustrate the point: challenge: output the system time. Fictional stack based esolang #1: St (system call "time" + implicit print -> 2 bytes). Fictional stack based esolang #2: No systems calls available, therefore the time is expected as input on top of the stack + implicit print -> 0 bytes). #2 wins, because #1 has to use the system calls. Same example for non-esolangs: import Time;f=print(system_time()) vs. f(x)=print(x). \$\endgroup\$ – nimi Dec 12 '17 at 14:36
  • \$\begingroup\$ @nimi That falls afoul of this loophole. \$\endgroup\$ – Mego Dec 12 '17 at 14:38
  • \$\begingroup\$ @Mego: True for my mini example. However, the advantage for #2 remains even if the task is more complicated. \$\endgroup\$ – nimi Dec 12 '17 at 14:44
-9
\$\begingroup\$

Programs running under Unix may produce output by overwriting their command-line arguments, which changes what ps reports.

I'm posting this because it was brought up as a loophole in my answer to this question. I personally think it should not be allowed, because it's obscure, not available in all languages, and ephemeral (someone has to run ps while the program is still executing).

\$\endgroup\$
  • \$\begingroup\$ this seems to be a special case of "functions may use out parameters" \$\endgroup\$ – Jasen Dec 22 '16 at 10:55
  • \$\begingroup\$ but ps doesn't report it after the program ends :( \$\endgroup\$ – Jasen Dec 22 '16 at 11:01
-20
\$\begingroup\$

Dynamic creation of a "fake" STDIN, when one isn't present (creating also a separation between the code and the input).


For example, Javascript doesn't have STDIN(for obvious reasons).

One could make something like function(a){[code here]} and only count the content inside it + 1.

On php, one can read from STDIN using $f=fread(STDIN,1024); and this would count only as 2 chars.

On other languages, this would vary a bit.

Any questions, I may answer on the comments.

\$\endgroup\$
  • 3
    \$\begingroup\$ Isn't the PHP example actually reading from STDIN? In which case, why shouldn't all of those bytes count? \$\endgroup\$ – Martin Ender Nov 3 '14 at 0:09
  • \$\begingroup\$ Depending on the way you handle it, if you read everything on STDIN at once, you can consider it a single input. I don't know if I'm making myself clear. But if your code is restricted to only 1 read to the STDIN, you can only consider the size in bytes of the name of the var. If multiple accesses are made (loops, conditions, partial reads...), everything is counted on the code. \$\endgroup\$ – Ismael Miguel Nov 3 '14 at 0:14
  • 1
    \$\begingroup\$ @MartinBüttner He's referring to the fact that different languages have different character counts for an STDIN read. Also, shouldn't this be CW? \$\endgroup\$ – TheDoctor Nov 4 '14 at 23:42
  • 3
    \$\begingroup\$ @TheDoctor Doesn't matter anyway - no rep gain/loss for meta. \$\endgroup\$ – Hosch250 Nov 6 '14 at 21:44
-27
\$\begingroup\$

Output is saved in a certain variable at the time the program has finished execution

This eliminates the disadvantages languages like Java have when printing System.out.println("asdf")

For languages without variables this could be top of the stack etc...

\$\endgroup\$
  • 23
    \$\begingroup\$ This essentially makes the submission a snippet instead of a full program, which the other meta question decided should not be a valid default. \$\endgroup\$ – Martin Ender Sep 11 '15 at 18:25
  • \$\begingroup\$ @MartinBüttner Unless the programming language is, of course, a turing machine. (I do vote for placing the tape head at the return value) \$\endgroup\$ – YoYoYonnY Jan 31 '16 at 0:41
  • 2
    \$\begingroup\$ In C#, a lambda is more a variable than a function. Lambdas are build on top of Delegates. Are they still valid? (By this, i think so) \$\endgroup\$ – aloisdg Jul 6 '16 at 20:32
-35
\$\begingroup\$

Programs may take input by assuming it is stored in predefined variables.

\$\endgroup\$
  • 4
    \$\begingroup\$ -1 (Can't vote) \$\endgroup\$ – Nathan Merrill Mar 17 '16 at 21:43
  • 1
    \$\begingroup\$ This just skips the "reading" step of a program. I don't think programs should be allowed to assume the output of an arbitrary computation, e.g. the one asked for in the question, is stored in a predefined variable either. \$\endgroup\$ – djechlin Jun 6 '16 at 18:11
  • 1
    \$\begingroup\$ @djechlin we are intentionally separating input/output methods. Outputting on variables is listed here: meta.codegolf.stackexchange.com/a/6965/20198 \$\endgroup\$ – Nathan Merrill Jun 6 '16 at 18:14
  • \$\begingroup\$ Okay, I still don't think you can skip the "reading" step of a program any more than you can skip the "writing" step of a program. That's all I'm trying to say. \$\endgroup\$ – djechlin Jun 6 '16 at 18:15
  • 2
    \$\begingroup\$ I'm confused what you are talking about. Programs have input and output. We define valid input methods (such as reading from a file, or parameters to a function), and valid output methods (writing to a file, or return values). This post is directly targeted at input methods. There is another answer on this question targeted at output methods. We split them so people can vote separately. \$\endgroup\$ – Nathan Merrill Jun 6 '16 at 18:18
  • 1
    \$\begingroup\$ This is most relevant to the Sinclair ZX80 and ZX81 BASIC. \$\endgroup\$ – Shaun Bebbers Apr 4 '17 at 9:04

You must log in to answer this question.

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