128
\$\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.


Return to FAQ index

\$\endgroup\$
2
  • \$\begingroup\$ Related (Can numeric input/output be in the form of byte values) \$\endgroup\$
    – mbomb007
    Commented Dec 13, 2016 at 16:10
  • 6
    \$\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
    Commented Jul 18, 2018 at 18:17

118 Answers 118

153
\$\begingroup\$

Functions may output via their return value(s)

\$\endgroup\$
9
  • 1
    \$\begingroup\$ How many languages support this? I support outputting to STDOUT. \$\endgroup\$
    – user10766
    Commented Nov 3, 2014 at 0:16
  • 36
    \$\begingroup\$ @hosch250 Wait, are there languages that have functions without return values? o.O \$\endgroup\$ Commented Nov 3, 2014 at 0:17
  • \$\begingroup\$ Oh, that is not considered outputting the last I heard - that is returning a value. If the question asks for a function, this is correct. If the question asks for a program, I support outputting to STDOUT or a file. \$\endgroup\$
    – user10766
    Commented Nov 3, 2014 at 0:19
  • 3
    \$\begingroup\$ @hosch250 This particular answer only concerns functions. Whether functions and/or programs are allowed is another question (see the linked meta question, but of course it's ultimately up to the challenge author). But this answers says that if functions are allowed, then they should be allowed to use their return value for the result - as are all other answers here that start with the word Functions. \$\endgroup\$ Commented Nov 3, 2014 at 0:21
  • \$\begingroup\$ Oh, sorry. I definitely will vote on this then. \$\endgroup\$
    – user10766
    Commented Nov 3, 2014 at 0:22
  • 1
    \$\begingroup\$ @MartinEnder Most (all?) 8-bit BASIC interpreters do not specifically return a value from a sub-routine, whilst many will allow you to define a mathematical function with the DEF keyword, this is quite limited. I also don't recall Amiga BASIC included with WorkBench having return values from sub-routines but it's been many years since I did any Amiga BASIC so don't quote me on that. 6502/10 assembly does not return values from sub-routines either. If you know that you've stored your result in the accumulator, then you simply use that on return from your sub-routine. \$\endgroup\$ Commented Apr 4, 2017 at 8:58
  • 2
    \$\begingroup\$ @MartinEnder Yes, scratch has no return values for it's functions \$\endgroup\$ Commented Aug 27, 2017 at 15:51
  • 1
    \$\begingroup\$ @Hosch250 There are languages that don't have STDOUT, like JS \$\endgroup\$
    – Luvexina
    Commented Jul 3, 2019 at 20:39
  • 1
    \$\begingroup\$ @VFDan In case of JS, console.log-ing is often regerded as STDOUT. Also, Node.js does have a STDOUT. \$\endgroup\$
    – FZs
    Commented May 3, 2021 at 6:43
133
\$\begingroup\$

Functions may take input via function arguments

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I use this in a lot of my questions, so definitely. \$\endgroup\$
    – Joe Z.
    Commented Nov 16, 2014 at 22:43
  • 2
    \$\begingroup\$ Can it take array via repeatedly call a function, once a value? \$\endgroup\$
    – l4m2
    Commented Dec 18, 2017 at 21:39
  • 3
    \$\begingroup\$ @l4m2 that sounds like a special form of currying and would be worth asking about in a separate question. (In principle, this should be valid, but it's worth discussing explicitly.) \$\endgroup\$
    – Martin Ender Mod
    Commented Dec 18, 2017 at 21:40
130
\$\begingroup\$

Programs may take input via STDIN

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

Programs may output to STDOUT

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

Programs may take input via GUI prompts

(This is for languages, for which this is the closest alternative to STDIN, like JavaScript's prompt(), Mathematica's Input[] or InputString[], Matlab's input() and VBScript's InputBox().)

\$\endgroup\$
7
  • 1
    \$\begingroup\$ What about for other languages? \$\endgroup\$
    – Ypnypn
    Commented Nov 4, 2014 at 0:32
  • \$\begingroup\$ @Ypnypn I'm not aware of a language which can read from STDIN, but where producing a graphical prompt is shorter than that. But in that case, I'd say either is fine. \$\endgroup\$ Commented Nov 4, 2014 at 9:22
  • 1
    \$\begingroup\$ @MartinBüttner Matlabs input('') usually just reads from the command line, perhaps you mean inputdlg('')? \$\endgroup\$
    – flawr
    Commented Sep 11, 2015 at 16:29
  • \$\begingroup\$ Anyway, Mma's Input and InputString both take the next line of input when run in a script. \$\endgroup\$ Commented Nov 8, 2015 at 12:35
  • \$\begingroup\$ @MartinEnder wscript. \$\endgroup\$
    – wizzwizz4
    Commented Jul 21, 2016 at 15:47
  • 1
    \$\begingroup\$ Bash read <var> \$\endgroup\$
    – Stan Strum
    Commented Jan 7, 2018 at 22:09
  • 1
    \$\begingroup\$ So reading choice from confirm and msgbox etc. are also allowed \$\endgroup\$
    – l4m2
    Commented Mar 10, 2018 at 8:56
102
\$\begingroup\$

Programs may output by displaying it on screen.

This makes it possible to use languages like Vim script that can't print output directly to stdout.

Example from this challenge:

$ echo "This is a test line!" | vim - -c 'nm Q vEUWvEuWQ|norm Q'

Will display:

THIS is A test LINE!
~
~
~
~
~

Within a Vim session.

I think that this way of displaying the result should be valid. I am sure that there are other domain specific languages like Vim script that lacks full I/O support and could use another way to display the result.

\$\endgroup\$
2
  • 4
    \$\begingroup\$ Incidentally, vim's output is standard output, but if you processed standard output, there is extra garbage there. \$\endgroup\$
    – Joshua
    Commented Jan 25, 2016 at 20:04
  • 3
    \$\begingroup\$ It's not even just domain specific languages. I'd say QBasic falls in this category: it doesn't have any concept of an output (or input) stream, but rather writes characters to a "window." \$\endgroup\$
    – DLosc
    Commented Sep 3, 2016 at 3:51
97
\$\begingroup\$

Programs may output using their exit code...

Exit codes are basically a return value for programs. If functions can output using their return values, it makes sense that programs should be able to do the same.

Examples:

\$\endgroup\$
7
  • \$\begingroup\$ Hmm…should this perhaps be "if and only if the challenge requires as output an integer whose value is guaranteed to be between 0 and 255, inclusive"? For example, if the challenge is "output the number of arguments passed to the program," then int main(int argc, char **argv) { return argc; } should not be a valid solution (IMO), because the exit code is masked by 0xFF. (Perhaps this is implicit in the rule as a part of correctness? Just want to be clear.) \$\endgroup\$
    – wchargin
    Commented Dec 18, 2016 at 5:05
  • 6
    \$\begingroup\$ @wchargin That seems like a limitation of whatever you are using to run your program. I can write a PowerShell script like exit 1000000, and execute it in PowerShell ISE. This gives me no problems. \$\endgroup\$
    – Rainbolt
    Commented Dec 19, 2016 at 19:14
  • 3
    \$\begingroup\$ Interesting—I didn't know that. As far as I know, this is true on all *nix OSes (including Mac), but I'd be happy to be corrected! \$\endgroup\$
    – wchargin
    Commented Dec 19, 2016 at 19:21
  • \$\begingroup\$ is there a reason to limit this to integers? i mean if there's an environment where a program can return something other than an integer why not allow that too? \$\endgroup\$
    – Jasen
    Commented Dec 22, 2016 at 10:21
  • \$\begingroup\$ @Jasen I don't see a reason to limit it to integers. \$\endgroup\$
    – Rainbolt
    Commented Dec 22, 2016 at 14:04
  • \$\begingroup\$ I'm new to this site. Would a crashed program's exit code considered a valid way of output (for example, SIGSEGV)? \$\endgroup\$
    – Shmuel H.
    Commented Jan 9, 2017 at 16:04
  • 6
    \$\begingroup\$ @ShmuelH. If you intentionally crash a program (or just use the equivalent of exit(code) in that language) to get a specific exit code, it's valid. \$\endgroup\$
    – user45941
    Commented Jan 9, 2017 at 16:31
86
\$\begingroup\$

Programs may take input via command-line arguments

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

Functions may take multiple arguments via currying

For some functional programming languages like Haskell this is actually necessary, because only single-argument functions exist and functions with multiple arguments are (somewhat transparently) implemented as curried functions. (The alternative would be to take a list or tuple of the values, but that is not how one would naturally write a two-argument function in Haskell.)

As per this consensus the same should also be allowable for languages where multi-argument functions do exist. As an example from JavaScript, instead of defining

f=(a,b)=>...

and calling it like f(a,b) one could then also define

f=a=>b=>...

and call it like f(a)(b).

\$\endgroup\$
1
  • \$\begingroup\$ Isn't that how lambda calculus works? \$\endgroup\$ Commented Aug 13 at 11:09
82
\$\begingroup\$

The contents of the tape post-execution may be used as a Turing machine's output

\$\endgroup\$
2
  • 7
    \$\begingroup\$ This has my upvote. \$\endgroup\$ Commented Sep 22, 2015 at 20:54
  • 6
    \$\begingroup\$ Emphasis on the "post-execution". If you allow the final tape of a non-halting turing machine to be considered out, your program can calculate a non-computable function. \$\endgroup\$ Commented Dec 10, 2018 at 14:15
70
\$\begingroup\$

Functions may output via the same methods as full programs

(This depends on how the poll goes, but could be any subset of STDOUT, STDERR and file.)

\$\endgroup\$
7
  • \$\begingroup\$ This violates the single-responsibility principle, the separation of concerns principle, and I don't think it's really useful in code golf. Moreover, functional programming languages won't let you do that at all, and I want some more love for Haskell. \$\endgroup\$ Commented Nov 3, 2014 at 6:42
  • 4
    \$\begingroup\$ @JanDvorak One simple golf use is that print is shorter than return in Python. \$\endgroup\$
    – xnor
    Commented Nov 3, 2014 at 8:57
  • 5
    \$\begingroup\$ Eugh. Not a fan of that optimisation. What it could be useful for is printing in a loop rather than collecting results in an array. Still not a fan of allowing that. \$\endgroup\$ Commented Nov 3, 2014 at 9:02
  • \$\begingroup\$ @JanDvorak Yeesh, I did not consider that one could print repeatedly to "output" a list. I think that should not be allowed even if this vote passes because the output is not the list that is asked for. \$\endgroup\$
    – xnor
    Commented Nov 3, 2014 at 9:06
  • 12
    \$\begingroup\$ @xnor Why not, full programs are allowed to do that, too, right? \$\endgroup\$ Commented Nov 3, 2014 at 11:22
  • \$\begingroup\$ @MartinBüttner Hmm, you're right, what a program really "outputs" is the string representation of the result, so this rule should mean functions can too for consistency. But this is another awkward consequence that makes me disfavor this rule. \$\endgroup\$
    – xnor
    Commented Nov 4, 2014 at 2:15
  • \$\begingroup\$ what about exit codes? aren't functions suposed to be reusable? if my function exits the program it isn't reusable. that's what the title indicates \$\endgroup\$ Commented Oct 26, 2017 at 13:14
66
\$\begingroup\$

Programs may output to STDERR

\$\endgroup\$
8
  • 11
    \$\begingroup\$ Can anyone give a comment as to why not to? For example, print("Hello, world!") in lua prints to STDERR. \$\endgroup\$
    – yyny
    Commented Jan 31, 2016 at 0:40
  • 7
    \$\begingroup\$ Forbidding output to STDERR seems incompatible with allowing output to the screen. \$\endgroup\$
    – Dennis Mod
    Commented May 2, 2016 at 18:47
  • 1
    \$\begingroup\$ This should go as standar... Ah. I see. \$\endgroup\$
    – wizzwizz4
    Commented Jul 21, 2016 at 16:00
  • \$\begingroup\$ The only problem I see with this is vagueness between what is written to STDERR by the "language" and what is written by the "program." If a distinction between those is not made, this would permit error quines and answers like this, which is questionably valid. \$\endgroup\$ Commented Dec 29, 2016 at 18:29
  • \$\begingroup\$ I think this shouldn't be allowed by default (but could be explicitly allowed on some challenges). I believe this goes against the spirit of some challenges such as this. On the other hand it could be explicitly forbidden but not everybody will think of this \$\endgroup\$
    – drolex
    Commented Jan 27, 2017 at 12:36
  • 9
    \$\begingroup\$ This is game-breaking for PHP, where <?=a; outputs a to STDOUT and Notice: Use of undefined constant a - assumed 'a' in [...][...] on line 3 to STDERR. \$\endgroup\$ Commented Jan 27, 2017 at 14:35
  • 5
    \$\begingroup\$ @IsmaelMiguel This means that outputting to STDERR only counts if that's your chosen output format. \$\endgroup\$
    – MilkyWay90
    Commented Apr 6, 2019 at 22:31
  • 2
    \$\begingroup\$ @YoYoYonnY Since when? All Lua 5.1-5.3 output to stdout… \$\endgroup\$ Commented Jul 17, 2019 at 14:16
63
\$\begingroup\$

Functions may output by modifying their arguments or writing to out arguments

Here, "out arguments" refers to arguments that are passed in by reference, so that modifying the value inside the function (as opposed to just overwriting the argument) also modifies the value outside the function (otherwise, the output value would not be observable after the function returns). Note that this is not possible in all languages with all types.

\$\endgroup\$
8
  • 3
    \$\begingroup\$ Does this mean that in languages that allow it, the seven characters for return can simply be replaced with an assignment? \$\endgroup\$
    – lirtosiast
    Commented Jun 10, 2015 at 21:05
  • 9
    \$\begingroup\$ @ThomasKwa Yes, provided the changed value will then be accessible in the context that called the function. \$\endgroup\$
    – Martin Ender Mod
    Commented Jun 10, 2015 at 21:18
  • 2
    \$\begingroup\$ so you mean this is only legal in languages that have out arguments, pass by reference, or some similar semantic? for instance not in shell... \$\endgroup\$
    – Jasen
    Commented Dec 22, 2016 at 10:27
  • \$\begingroup\$ @Jasen I don't know about shell, but yes to your question. \$\endgroup\$
    – Martin Ender Mod
    Commented Dec 22, 2016 at 10:31
  • \$\begingroup\$ This should be clarified to say that you have to actually modify the argument, not just overwrite it. See this for an incorrect use. \$\endgroup\$
    – isaacg
    Commented Jun 28, 2017 at 9:40
  • \$\begingroup\$ @isaacg Good point, thank you. \$\endgroup\$
    – Martin Ender Mod
    Commented Jun 28, 2017 at 9:46
  • \$\begingroup\$ @isaacg you need to link to a specific revision for that comment to make sense. \$\endgroup\$ Commented Jun 30, 2017 at 19:58
  • 1
    \$\begingroup\$ The revision that I referred to about is codegolf.stackexchange.com/revisions/128782/6 \$\endgroup\$
    – isaacg
    Commented Jun 30, 2017 at 23:35
60
\$\begingroup\$

For stack-based languages, function's input may be pushed to the stack before calling

Stack-based languages may assume that the input for their function is automatically pushed to the stack.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ +1, this is the de facto standard for CJam and GolfScript (and probably some other stack-based languages). You might want to make a corresponding answer for output. \$\endgroup\$
    – Martin Ender Mod
    Commented Feb 19, 2016 at 13:29
  • 7
    \$\begingroup\$ I think the emphasis here needs to be on "function", as opposed to snippet. There's a very fine line between the two... \$\endgroup\$
    – Sp3000
    Commented Feb 19, 2016 at 13:34
  • 1
    \$\begingroup\$ Are there any stack based langauges that let you define functions? As far as I am aware most of them do not and do only let you write full programs with IO via STDIO. (in my opinion functions should satisfy (perhaps among other criterions): pieces of code that can be reused in the same program, without typing them out again) \$\endgroup\$
    – flawr
    Commented Jul 10, 2016 at 16:27
  • \$\begingroup\$ @flawr Vitsy has functions, in some aspects. There are a few stack-based languages that have scope, but I can't name them immediately. \$\endgroup\$ Commented Jul 11, 2016 at 4:12
  • 4
    \$\begingroup\$ @flawr for completion sake: lots (most?) of non-esoteric stack-based languages let you define functions. Factor, Forth, Postscript, Joy, Cat, and many others all have functions (both named and anonymous). dc (the RPN calculator) has registers that can work like functions. \$\endgroup\$
    – fede s.
    Commented Sep 14, 2016 at 17:35
  • \$\begingroup\$ @flawr: Underload lets you reuse the functions in question (in a kind-of awkward way, as Underload has no method of naming things, but it's possible and fairly common). \$\endgroup\$
    – user62131
    Commented Jan 15, 2017 at 17:05
  • 1
    \$\begingroup\$ Would this apply to a language that has explicit but no implicit IO? \$\endgroup\$
    – Adalynn
    Commented Aug 5, 2017 at 17:19
59
\$\begingroup\$

Programs may output to a file

\$\endgroup\$
5
  • 12
    \$\begingroup\$ Why are people against this? \$\endgroup\$
    – xnor
    Commented Nov 8, 2014 at 5:48
  • 8
    \$\begingroup\$ @xnor is /dev/null considered a valid output file? I might as well submit a blank program and claim it outputs the correct answer to /dev/null. \$\endgroup\$ Commented Feb 15, 2016 at 21:55
  • 16
    \$\begingroup\$ @PatrickRoberts No, /dev/null is not a valid output file, for the exact reason that the output cannot be examined later. "Output to a file" means that you can see the program's output by viewing a particular file. \$\endgroup\$
    – DLosc
    Commented Sep 3, 2016 at 4:05
  • 19
    \$\begingroup\$ using strace (or chroot, or mv!) you can see what was written to /dev/null \$\endgroup\$
    – Jasen
    Commented Dec 22, 2016 at 10:30
  • 5
    \$\begingroup\$ @PatrickRoberts But it doesn't output to /dev/null \$\endgroup\$
    – MilkyWay90
    Commented Apr 6, 2019 at 22:28
57
\$\begingroup\$

Functions may take input via the same methods as full programs

(This depends on how the poll goes, but could be any subset of STDIN, ARGV and file.)

\$\endgroup\$
4
  • \$\begingroup\$ This violates the single-responsibility principle, the separation of concerns principle, and I don't think it's really useful in code golf. Moreover, functional programming languages won't let you do that at all, and I want some more love for Haskell. \$\endgroup\$ Commented Nov 3, 2014 at 6:43
  • 24
    \$\begingroup\$ @JanDvorak I don't see the relevance of good coding practices for code golf. And some languages don't even have functions and they aren't necessarily at a disadvantage by allowing functions. The idea of being more liberal in the options for I/O is so that each language can pick what's really shortest. \$\endgroup\$ Commented Nov 3, 2014 at 11:10
  • 2
    \$\begingroup\$ The only time that I would object to a function taking input in the same way as a program is if the question was phrased "Write a function that takes the following arguments". This poll applies to questions that are not in such a specific format, so I see no reason to exclude functions that read from stdin. \$\endgroup\$ Commented Nov 4, 2014 at 13:16
  • \$\begingroup\$ @MartinBüttner noted. I still don't like this option, but feel free to override my preferences. The vote split is very close to the threshold ATM. \$\endgroup\$ Commented Nov 7, 2014 at 9:47
55
\$\begingroup\$

Programs may combine two or more input methods

For example, if the inputs are a string and an int, a function that takes a string as an argument and an int from STDIN would be valid.

The input format must still be consistent for a given program.

\$\endgroup\$
53
\$\begingroup\$

Input may be entirely ignored

If a challenge specifies that your program should take a certain selection of inputs, but not all of them are necessary for you to solve the task, you can simply not take the ones you don't need. (This is equivalent to taking them via a method that your language doesn't look at, such as via command-line argument in a language which has no access to the command line arguments.)

\$\endgroup\$
2
  • \$\begingroup\$ Are you implying that if the challenge was "take three numbers as input a, b, and c, and return a+b", then print(input()+input()) would be a valid Python submission? \$\endgroup\$ Commented May 23, 2017 at 3:20
  • 7
    \$\begingroup\$ @Challenger5: Right. Also that that would be a valid submission if the request was to return a+c. (Even without this, you could still do that by saying, for example, "a and c are input via standard input, and b from a file". This rule's basically meant to avoid the absurdity of having to do that.) \$\endgroup\$
    – user62131
    Commented May 23, 2017 at 3:44
52
\$\begingroup\$

Where applicable, Turing machines supporting multiple halt states may also output via their halt state

This is essentially equivalent to programs outputting via their exit code.

\$\endgroup\$
3
  • 4
    \$\begingroup\$ This has my upvote. \$\endgroup\$ Commented Sep 22, 2015 at 20:54
  • \$\begingroup\$ Turing Machines as I've seen them defined leave output on their tape and have a single halt state. Is there some precedent for what you are suggesting? \$\endgroup\$
    – xnor
    Commented Sep 24, 2015 at 16:51
  • \$\begingroup\$ @xnor: That is usually the case, but I have seen dialects of turing machine code that do support multiple halt states. (For example, in the morphett.info dialect that I usually use, halt is the basic halt state, but any state of the form halt-<any string> is also treated as a halt state.) \$\endgroup\$ Commented Sep 24, 2015 at 19:09
51
\$\begingroup\$

Input for Turing machines may be written to the tape pre-execution

The read-write head should start on the leftmost cell of the portion of the tape containing the input.

\$\endgroup\$
6
  • 3
    \$\begingroup\$ This has my upvote. \$\endgroup\$ Commented Sep 22, 2015 at 20:53
  • 6
    \$\begingroup\$ I think this should only be allowed if that's the only method of input. \$\endgroup\$
    – mbomb007
    Commented Oct 8, 2015 at 20:39
  • 3
    \$\begingroup\$ For standard turing machines, it is. \$\endgroup\$ Commented Oct 8, 2015 at 20:59
  • 4
    \$\begingroup\$ What happened here, SuperJedi? You posted an answer, then commented "This has my upvote" an hour later? \$\endgroup\$
    – MD XF
    Commented Oct 31, 2017 at 16:55
  • 3
    \$\begingroup\$ @MDXF At the time, several other answers on this question had comments like that added by their authors (including many of Martin's answers). At least one was instead commented "This has my downvote." Basically, it was intended as a way to vote on your own meta proposals because you can't actually upvote or downvote your own answers on this site. Interestingly, most of those comments on this topic seem to have since been removed. \$\endgroup\$ Commented Nov 1, 2017 at 11:22
  • 2
    \$\begingroup\$ @SuperJedi224 Ah, okay, thanks. I was suspecting sock action, but that makes sense. \$\endgroup\$
    – MD XF
    Commented Nov 2, 2017 at 16:37
42
\$\begingroup\$

SQL submissions may take input from a named table

which is probably not good enough. But I don't know a better way.

\$\endgroup\$
1
  • \$\begingroup\$ Should be OK, as long as that input is treated as data, not executable code \$\endgroup\$
    – roblogic
    Commented Aug 12, 2019 at 9:03
41
\$\begingroup\$

Functions may return a list via acting as a generator

In other words, writing a function to return list elements lazily on request, using support that the language already has for doing that, is allowed (currently it's frequently seen but something of a gray area). This would be idiomatic in most of the languages which have it.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ I wasn't aware this was even a point of contention... \$\endgroup\$
    – user45941
    Commented Nov 30, 2016 at 21:57
  • 1
    \$\begingroup\$ I don't think it is, but we don't have it listed, and converting a generator to a list is fairly expensive byte-wise in Prolog (there's a builtin for it but it has a fairly long name). So I wanted to confirm that I don't have to do the conversion in my Prolog answers. \$\endgroup\$
    – user62131
    Commented Nov 30, 2016 at 23:03
  • \$\begingroup\$ Some things in Python 2 return a list, but in Python 3 it return a generator. Like range. \$\endgroup\$
    – mbomb007
    Commented Mar 3, 2017 at 22:46
  • \$\begingroup\$ @mbomb007 I think Python 3s range is equivalent to Python 2s xrange and Python 2s range is something different... \$\endgroup\$ Commented Aug 27, 2017 at 18:44
  • \$\begingroup\$ Basically. Python 2's range is like calling list() on the result. \$\endgroup\$
    – mbomb007
    Commented Aug 27, 2017 at 19:24
  • \$\begingroup\$ Maybe not in fastest code… \$\endgroup\$ Commented Aug 17, 2023 at 16:33
40
\$\begingroup\$

Programs may take input as the value of the last expression

Using something like TI-84 BASIC's Ans, which is a variable that stores the value of the last expression. This the shortest way for TI-BASIC to take input, but its validity was recently questioned. To be clear, this is how the process works with Ans:

  • Within a program, type the input (number, list, string, etc.), followed by a newline to separate statements, followed by the program name, then press Enter. For example,

    1337
    prgmFACTOR
    
  • Outside a program, type the input, press Enter, then type the program name and press Enter.

Edit: I came across a way of using Ans that is similar to command-line arguments, which are allowed: All as one expression, type the input, followed by a colon, followed by pgrmNAMEHERE. For example, 1337:pgrmFACTOR.

\$\endgroup\$
11
  • 4
    \$\begingroup\$ I think by definition that submission would not be a full program but a snippet that requires some sort of REPL environment which are disallowed by default. \$\endgroup\$
    – Martin Ender Mod
    Commented Feb 25, 2016 at 22:53
  • 9
    \$\begingroup\$ I think that TI-BASIC is a special case, it being fundamentally different from other languages, and only being callable from what can be considered a REPL; so what then is to differ a full program from a snippet? \$\endgroup\$ Commented Feb 29, 2016 at 17:06
  • 4
    \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ I disagree, you can write full programs in the REPL but you can also write them in separate "files" that can be executed. They even support arguments when the program is called, but can also read from stdin or via prompt. \$\endgroup\$
    – flawr
    Commented Mar 5, 2016 at 12:55
  • 3
    \$\begingroup\$ @flawr On the 84+ series they can't take arguments, and there's no STDIN, only a prompt. \$\endgroup\$
    – lirtosiast
    Commented Mar 15, 2016 at 16:48
  • 4
    \$\begingroup\$ @lirtosiast According to the official TI 84+ guidebook both commands Input and Prompt are available. Both let the program fetch user input. Both behave alot like e.g. input() in Python or MATLAB. \$\endgroup\$
    – flawr
    Commented Mar 15, 2016 at 17:59
  • 1
    \$\begingroup\$ @flawr I agree about their similarity; what I meant is that they display a ? that prompts the user to type something. \$\endgroup\$
    – lirtosiast
    Commented Mar 15, 2016 at 18:00
  • 1
    \$\begingroup\$ @lirtosiast Is that an issue? I think many languages do have symbols to denote new inputs/new lines. \$\endgroup\$
    – flawr
    Commented Mar 15, 2016 at 18:03
  • 1
    \$\begingroup\$ @flawr It's not an issue. \$\endgroup\$
    – lirtosiast
    Commented Mar 15, 2016 at 18:04
  • 4
    \$\begingroup\$ @flawr It's worth noting that you can't write full programs in the REPL, at least for the TI-83 and -84 calculators. For example, the Input/Prompt and looping commands can't be used from the REPL. \$\endgroup\$
    – Jakob
    Commented Jan 24, 2017 at 16:41
  • 3
    \$\begingroup\$ Does this mean using _ for the Python REPL is valid? \$\endgroup\$ Commented Feb 9, 2017 at 8:02
  • 2
    \$\begingroup\$ @JakobCornell There are actually assembly programs that let you bypass that \$\endgroup\$ Commented Mar 27, 2017 at 13:46
39
\$\begingroup\$

Assembly programs may take input from registers

If there are no I/O devices available, an answer might consist of a subroutine that reads its input values from the machine registers.

\$\endgroup\$
6
  • \$\begingroup\$ There's potential for abuse. What if someone creates a machine with a million different special-purpose registers? \$\endgroup\$
    – lirtosiast
    Commented Mar 15, 2016 at 17:10
  • 8
    \$\begingroup\$ @lirtosiast Good for code colf then. I don't see how that's different from golfing languages with prefilled vars \$\endgroup\$
    – cat
    Commented Jul 6, 2016 at 0:31
  • 2
    \$\begingroup\$ @lirtosiast I believe the programming language used has to be defined before the challenge starts. \$\endgroup\$
    – yyny
    Commented Dec 31, 2016 at 21:19
  • 4
    \$\begingroup\$ If you golf with x86-64, the standard calling conventions already pass args in registers. So you can just write a normal function that's callable from compiler-generated code. \$\endgroup\$ Commented Jan 1, 2017 at 7:06
  • 4
    \$\begingroup\$ This needs clarification: can we use any register, or only the ones that would be specified to be used by the ABI? \$\endgroup\$
    – user62131
    Commented Jun 11, 2017 at 4:14
  • \$\begingroup\$ Update on calling conventions: you don't have to use a standard C calling convention. Your language is assembly / machine code, so presumably that's the caller's language as well. They can pass args and take return value(s) in any registers including FLAGS on machines with condition codes, as discussed in Tips for golfing in x86/x64 machine code \$\endgroup\$ Commented Jul 21, 2023 at 11:39
36
\$\begingroup\$

Functions may return a boolean value via the presence or absence of an error/exception

That is, crashing to return false, and not crashing to return true. This is the function equivalent of outputting via exit code, which is allowed for full programs (nearly all interpreters will return an exit code of 0 by default if the program doesn't crash, and nonzero if it does).

I realised this wasn't on the list while writing a post about how our I/O defaults applied to Brachylog; this method of outputting booleans is idiomatic in most declarative languages (i.e. it's something I'd be likely to write even if I weren't golfing), and it's still meaningful in other paradigms (much rarer, but it's nonetheless considered an acceptable programming technique in industry in languages like OCaml).

I'm not completely convinced either way about whether this should be accepted, although I'm currently mildly in favour (the main point in favour is that it's often the most natural way to do output even if you aren't golfing, and many languages have a try/catch or equivalent to convert it into a regular Boolean; the main point against is that it feels a bit cheap/abusable). However, we could definitely do with a ruling as to whether this is acceptable or not, as there are many cases in which it's going to be the shortest way to write the code, and thus it matters for how answers are written.

\$\endgroup\$
6
  • 9
    \$\begingroup\$ This is pretty much "output via exit code" for functions. +1 from me. \$\endgroup\$ Commented Mar 29, 2017 at 17:42
  • 8
    \$\begingroup\$ Not sure about this either way, but it would be a significant change for Python golfing. Especially so if a recursive function that keeps nesting is considered a stack overflow error, which is a bit weird since it's common to assume there's unlimited stack depth. \$\endgroup\$
    – xnor
    Commented Mar 30, 2017 at 8:58
  • \$\begingroup\$ If anything, I think this should only be allowed in languages with some way of handling errors (usually try/catch). Otherwise, it would be impossible to actually use the code in a program. \$\endgroup\$
    – 12Me21
    Commented Mar 2, 2018 at 2:20
  • \$\begingroup\$ @12Me21 I read "crash" as different to "error" or "exception", where "crash" is always non-recoverable. \$\endgroup\$
    – Οurous
    Commented Jul 30, 2018 at 21:03
  • 2
    \$\begingroup\$ Is there a contradiction here with our stance on function reusability, namely Do function submissions have to be reusable? and Should submissions be allowed to exit with an error? \$\endgroup\$
    – Dingus
    Commented Oct 19, 2021 at 10:57
  • \$\begingroup\$ More recent meta discussion that expands on my comment above: Functions throwing exceptions: an exception to the rules? \$\endgroup\$
    – Dingus
    Commented Aug 30, 2022 at 13:28
35
\$\begingroup\$

Functions in stack-based languages can leave the output on the stack

Mostly for completeness sake. This is the de-facto standard for CJam and GolfScript and probably other stack-based languages.

\$\endgroup\$
8
  • \$\begingroup\$ This is also how Forth works. \$\endgroup\$
    – mbomb007
    Commented Aug 3, 2016 at 15:18
  • \$\begingroup\$ Would this apply to a language that has explicit but no implicit IO? \$\endgroup\$
    – Adalynn
    Commented Aug 5, 2017 at 17:19
  • \$\begingroup\$ @Zacharý not sure what you mean? \$\endgroup\$
    – Martin Ender Mod
    Commented Aug 5, 2017 at 17:21
  • \$\begingroup\$ A language like Befunge or something similar. Or am I misunderstanding the concept of "function" in these kinds of languages? \$\endgroup\$
    – Adalynn
    Commented Aug 5, 2017 at 17:28
  • \$\begingroup\$ @Zacharý Befunge doesn't have functions so this answer doesn't apply. \$\endgroup\$
    – Martin Ender Mod
    Commented Aug 5, 2017 at 18:42
  • \$\begingroup\$ I disagree. I think they can leave it on the stack if implicit output is available. There is no way to check if it's left on the stack otherwise. \$\endgroup\$
    – MD XF
    Commented Oct 31, 2017 at 16:57
  • 1
    \$\begingroup\$ @MDXF Wouldn't that be equivalent to allowing functions in "normal" languages only to use their return value, if the language has implicit output for return values, otherwise there's no way to check if the return value actually has the correct output? \$\endgroup\$
    – Martin Ender Mod
    Commented Oct 31, 2017 at 17:52
  • \$\begingroup\$ Yeah, like BIT++ \$\endgroup\$
    – kepe
    Commented Sep 15, 2018 at 18:25
35
\$\begingroup\$

Assembly programs may write output to some specified memory location

If there are no I/O devices available, an answer might consist of a subroutine that writes its output values to some specified memory location (e.g. write a machine word to $0000).

\$\endgroup\$
2
  • \$\begingroup\$ I think this includes writing to a buffer for outputting multiple values. \$\endgroup\$
    – qwr
    Commented Jul 12, 2018 at 2:23
  • 1
    \$\begingroup\$ I feel like all these similar posts could be combined into something like "Programs may use registers/variables/memory locations for input/output if no input/output device is available". That would cover assembly programs as well as SQL and other higher level languages with the same problems. \$\endgroup\$
    – 12Me21
    Commented Dec 30, 2018 at 19:00
34
\$\begingroup\$

Input and output of a single value may be represented as a singleton list

For example, if the value was 4, [4] would be an acceptable representation (using Python list syntax).

\$\endgroup\$
1
  • 5
    \$\begingroup\$ This is useful in array languages like J. \$\endgroup\$ Commented Apr 26, 2017 at 16:51
33
\$\begingroup\$

Submissions may use list of strings instead of multi-line strings

Input or output may be the equivalent of the string separated by newlines. For example, the multiline string:

1
2
Fizz
4
Buzz

May be instead be taken as a list of strings like:

["1","2","Fizz","4","Buzz"]
\$\endgroup\$
2
  • 2
    \$\begingroup\$ I always see comments about this in multiline challenges, so I thought I'd throw this up there and see if it is well-recieved as a standard form of input \$\endgroup\$
    – Jo King Mod
    Commented Nov 13, 2018 at 11:54
  • 18
    \$\begingroup\$ I would add an exception for ascii-art as linebreaks can be an important part of those challenges. \$\endgroup\$
    – lirtosiast
    Commented Nov 19, 2018 at 19:43
32
\$\begingroup\$

In languages without any method of input (e.g. ///) programs may get input through an insertion into the source code

\$\endgroup\$
7
  • 3
    \$\begingroup\$ @DLosc What about code in /// that requires changing at the end? E.g. a simple program that adds a full stop to the end of a string if there isn't already a full stop there /.;/;//x///;/.x;/(input goes here); where x is a special character (maybe a newline) that is guaranteed not to be in the input. \$\endgroup\$
    – boboquack
    Commented Nov 20, 2016 at 9:34
  • 10
    \$\begingroup\$ This is called hard coding. \$\endgroup\$ Commented Feb 28, 2017 at 12:33
  • 1
    \$\begingroup\$ @boboquack Can we allow hard-coded input in languages that support input commands? \$\endgroup\$
    – user85052
    Commented Aug 25, 2019 at 11:38
  • 1
    \$\begingroup\$ That's a good question, and from experience I would guess no, but you should search on meta/ask your own question here. @A_ \$\endgroup\$
    – boboquack
    Commented Aug 26, 2019 at 4:05
  • \$\begingroup\$ Does this include 2d programming languages and/or cellular automata? \$\endgroup\$
    – rydwolf
    Commented Oct 2, 2020 at 20:35
  • 1
    \$\begingroup\$ @RedwolfPrograms It's been a long time since I've thought about this, but I would assume yes. \$\endgroup\$
    – boboquack
    Commented Oct 3, 2020 at 4:30
  • \$\begingroup\$ @boboquack I'm going to post a separate set of answers, as it seems different enough the consensus may differ. Thanks for the quick reply! \$\endgroup\$
    – rydwolf
    Commented Oct 3, 2020 at 14:26

You must log in to answer this question.

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