The Wayback Machine - https://web.archive.org/web/20160801214015/http://meta.codegolf.stackexchange.com:80/questions/2447/default-for-code-golf-input-output-methods

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.

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.

share

38 Answers 38

Functions may output via their return value(s)

share
1  
This has my upvote. – Martin EnderNov 2 '14 at 23:15
    
How many languages support this? I support outputting to STDOUT. – Hosch250Nov 3 '14 at 0:16
12  
@hosch250 Wait, are there languages that have functions without return values? o.O – Martin EnderNov 3 '14 at 0:17
    
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. – Hosch250Nov 3 '14 at 0:19
    
@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. – Martin EnderNov 3 '14 at 0:21
    
Oh, sorry. I definitely will vote on this then. – Hosch250Nov 3 '14 at 0:22

Programs may take input via STDIN

share
1  
This has my upvote. – Martin EnderNov 2 '14 at 23:09

Programs may output to STDOUT

share
1  
This has my upvote. – Martin EnderNov 2 '14 at 23:12
2  
did you have to do this to every single one you voted up? – TheDoctorNov 4 '14 at 23:25
5  
@TheDoctor I can't actually vote, because I posted the answers myself, so those comments are my votes. – Martin EnderNov 4 '14 at 23:28
1  
Ah that makes sense – TheDoctorNov 4 '14 at 23:38

Functions may take input via function arguments

share
1  
This has my upvote. – Martin EnderNov 2 '14 at 23:12
    
I use this in a lot of my questions, so definitely. – Joe Z.Nov 16 '14 at 22:43

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[] and Matlab's input().)

share
1  
This has my upvote. – Martin EnderNov 3 '14 at 0:11
1  
What about for other languages? – YpnypnNov 4 '14 at 0:32
    
@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. – Martin EnderNov 4 '14 at 9:22
    
@MartinBüttner Matlabs input('') usually just reads from the command line, perhaps you mean inputdlg('')? – flawrSep 11 '15 at 16:29
    
Anyway, Mma's Input and InputString both take the next line of input when run in a script. – LegionMammal978Nov 8 '15 at 12:35
    
@MartinEnder wscript. – wizzwizz4Jul 21 at 15:47

Programs may take input via command-line arguments

share
1  
This has my upvote. – Martin EnderNov 2 '14 at 23:11

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.

share
    
Incidentally, vim's output is standard output, but if you processed standard output, there is extra garbage there. – JoshuaJan 25 at 20:04

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.)

share
1  
This has my upvote. – Martin EnderNov 3 '14 at 1:56
    
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. – Jan DvorakNov 3 '14 at 6:42
1  
@JanDvorak One simple golf use is that print is shorter than return in Python. – xnorNov 3 '14 at 8:57
1  
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. – Jan DvorakNov 3 '14 at 9:02
    
@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. – xnorNov 3 '14 at 9:06
9  
@xnor Why not, full programs are allowed to do that, too, right? – Martin EnderNov 3 '14 at 11:22
    
@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. – xnorNov 4 '14 at 2:15

Programs may output using their exit code...

... if and only if the challenge requires an integer as output, obviously.

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:

share

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).

share
    
No, haskell can avoid currying by using tuple as the argument. – Christian IrwanFeb 19 at 10:39
1  
@ChristianIrwan True (if that is even allowed) - I clarified that part a bit. – Martin EnderFeb 19 at 10:53

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.)

share
1  
This has my upvote. – Martin EnderNov 3 '14 at 1:56
    
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. – Jan DvorakNov 3 '14 at 6:43
11  
@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. – Martin EnderNov 3 '14 at 11:10
1  
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. – trichoplaxNov 4 '14 at 13:16
    
@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. – Jan DvorakNov 7 '14 at 9:47

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

share
1  
This has my upvote. – Martin EnderMar 25 '15 at 10:51
2  
Does this mean that in languages that allow it, the seven characters for return can simply be replaced with an assignment? – lirtosiastJun 10 '15 at 21:05
5  
@ThomasKwa Yes, provided the changed value will then be accessible in the context that called the function. – Martin EnderJun 10 '15 at 21:18

Programs may output to a file

share
1  
This has my downvote. – Martin EnderNov 7 '14 at 9:45
6  
Why are people against this? – xnorNov 8 '14 at 5:48
3  
@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. – Patrick RobertsFeb 15 at 21:55

SQLs may take input from a named table

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

share

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

share
2  
This has my upvote. – SuperJedi224Sep 22 '15 at 20:54

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.

share

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

This is equivalent to programs outputting via their exit code.

share
1  
This has my upvote. – SuperJedi224Sep 22 '15 at 20:54
    
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? – xnorSep 24 '15 at 16:51
    
@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.) – SuperJedi224Sep 24 '15 at 19:09

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.

share
1  
This has my upvote. – SuperJedi224Sep 22 '15 at 20:53
3  
I think this should only be allowed if that's the only method of input. – mbomb007Oct 8 '15 at 20:39
1  
For standard turing machines, it is. – SuperJedi224Oct 8 '15 at 20:59

Programs may take input from a file

share
5  
... only if makes sense for the challenge, and even then you should input a file name first... I'd say – Jan DvorakNov 3 '14 at 6:32
2  
@JanDvorak That's a "no", because that makes the file name itself the input (which you would take from STDIN or ARGV). – Martin EnderNov 3 '14 at 11:07
1  
This has my downvote. – Martin EnderNov 7 '14 at 9:45

Input for stack-based language's functions 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.

share
1  
+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. – Martin EnderFeb 19 at 13:29
2  
I think the emphasis here needs to be on "function", as opposed to snippet. There's a very fine line between the two... – Sp3000Feb 19 at 13:34
    
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) – flawrJul 10 at 16:27
    
@flawr Vitsy has functions, in some aspects. There are a few stack-based languages that have scope, but I can't name them immediately. – VTCAKAVSMoACEJul 11 at 4:12

Regexes may output via the list of matched strings (the captured group 0)

share
1  
We now have Retina for regex so this can – CalculatorFelineApr 2 at 16:13

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.

share

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.

share
    
There's potential for abuse. What if someone creates a machine with a million different special-purpose registers? – lirtosiastMar 15 at 17:10
1  
@lirtosiast Good for code colf then. I don't see how that's different from golfing languages with prefilled vars – catJul 6 at 0:31

CGI scripts may take input via GET/POST parameters

A CGI script required to take, e.g., three inputs can be expected to be called as the GET request

GET script.cgi?<A>=<1st input>&<B>=<2nd input>&<C>=<3rd input> HTTP/1.1 

(where A, B and C are keys of the poster's choice) of the equivalent POST request.

share
    
Also, PHP<5.4.0 (exclude) has the directive register_globals (enabled by default up to PHP4.1), which allows you to use POST, GET, COOKIE, SESSION and what-not. It creates the variables with the names of the keys (E.g.: a.php?a=1 would create $_GET[a] (version-dependent) and the variable $a with value 1). This was removed due to security reasons, but was a standard feature that was (fortunally) removed. – Ismael MiguelNov 27 '15 at 17:52
    
Also, would parse_str($_SERVER[QUERY_STRING]) be a valid input method? It does exactly the same as register_globals, but reads input parameters from a superglobal and parse them into variables. This is explained in the documentation: php.net/manual/en/function.parse-str.php – Ismael MiguelNov 27 '15 at 18:01
    
This proposed a specific input method. What language features you use to access that input is up to you. – DennisNov 27 '15 at 18:20
    
Then using $_GET (superglobal with all GET parameters, in PHP) fits the "What language features you use to access that input is up to you", right? – Ismael MiguelNov 27 '15 at 18:25
    
Yes, it does, assuming a GET request becomes an accepted input input. – DennisNov 27 '15 at 18:37
    
Then there is the program <?php eval(key($_GET));. (SQL table input may have similar problems.) – jimmy23013Nov 27 '15 at 18:51
1  
@jimmy23013 PHP4.1: <?eval($c); (assuming short_tags and register_globals, the code can be anywhere, even in a cookie (if below 4kb)). Or even <?eval(fgets(STDIN)). Or, for Javascript, eval(prompt()). Or eval "$s" for bash. Other languages have the same problem. Any language that allows evaluation of a string containing code has the same problem – Ismael MiguelNov 27 '15 at 20:19
    
@Dennis Would eval(`return {${location.hash}}`), assuming your URL is http://example.com/#a:"value",other:"value", fit in this definition? – Ismael MiguelNov 27 '15 at 20:32
    
@IsmaelMiguel As written, no. Those are neither GET nor POST parameters. – DennisNov 27 '15 at 20:34
    
@Dennis What about eval(location.search.replace(/^\?/,''))? It would create every single variable you would need, right away (if the input is numeric and not an array). The location.search will contain all the GET parameters, optionally starting with an ?. – Ismael MiguelNov 27 '15 at 20:38
    
This can also be done with php -r '<code>' <A>=<input1> <B>=<input2> (or php-cli, depending on your distribution/installation), which alleviates the need for a web server (a much better option IMO). In this case, scoring is much easier: the -r flag would cost 2 bytes, but the ability to drop the leading <? in the code balances it out. Each GET parameter would cost 1 byte (for the =) plus the length of the parameter name. – MegoNov 28 '15 at 6:11
    
@Mego The -r is free. Read: meta.codegolf.stackexchange.com/q/2424/14732 – Ismael MiguelNov 28 '15 at 22:44
    
@IsmaelMiguel Fair enough. The rest of my comment still stands. – MegoNov 28 '15 at 23:42
    
@Mego It's still under discussion. – Ismael MiguelNov 29 '15 at 1:09
    
@IsmaelMiguel Hence why I offered my opinion – MegoNov 29 '15 at 1:58

Assembly programs may write output to registers

If there are no I/O devices available, an answer might consist of a subroutine that leaves its computed output values in the machine registers upon returning.

share

Assembly programs may read input from some specified memory location

If there are no I/O devices available, an answer might consist of a subroutine that reads its input values from some specified memory location (e.g. read a zero-terminated string starting at $0000).

share
    
This would be natural for various 8-bit micros. – zwolMar 1 at 4:50

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).

share

Regexes may output a number via the number of matches

Or truthy / falsey as whether they match.

share
7  
Plain regex submissions are still not generally accepted as valid so I'm not sure it's worth adding defaults for this very specific case that a lot of people don't accept at all. – Martin EnderMay 30 '15 at 10:27
1  
Again , Retina. – CalculatorFelineApr 2 at 16:14

Regexes may output via any captured groups

The users can specify which captured group(s) contain the output, not necessarily group 0.

share

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 pgrmWHATEVERTHENAMEIS. For example, 1337:pgrmFACTOR.

share
3  
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. – Martin EnderFeb 25 at 22:53
4  
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? – Cᴏɴᴏʀ O'BʀɪᴇɴFeb 29 at 17:06
4  
@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. – flawrMar 5 at 12:55
    
@flawr On the 84+ series they can't take arguments, and there's no STDIN, only a prompt. – lirtosiastMar 15 at 16:48
2  
@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. – flawrMar 15 at 17:59
    
@flawr I agree about their similarity; what I meant is that they display a ? that prompts the user to type something. – lirtosiastMar 15 at 18:00
1  
@lirtosiast Is that an issue? I think many languages do have symbols to denote new inputs/new lines. – flawrMar 15 at 18:03
1  
@flawr It's not an issue. – lirtosiastMar 15 at 18:04

You must log in to answer this question.

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

close