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

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.

• Related (Can numeric input/output be in the form of byte values) – mbomb007 Dec 13 '16 at 16:10
• "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. – Sparr Jul 18 '18 at 18:17

# Assembly language / machine code must take input and output as defined by an ABI for the platform that predates the challenge

For example, most commonly used ABIs for x86 state that 32-bit integer outputs are returned in the %eax register. As such, that's a valid way to output a 32-bit integer. However, outputting in, e.g., %esp would not be valid, unless a pre-existing ABI can be located where that's considered a valid method of output.

(Some background for people unaware of assembly language technology: an ABI, or Application Binary Interface, is a specification for how pieces of code within a particular program, on a particular platform, communicate with each other via the state of the processor. Most ABIs specify specific memory addresses and/or registers to be used for the communication.)

This is outright intended as a competing answer to the other assembly-language-related answers, because I feel they're too permissive and could lead to loopholes in which bytes are moved from the program to a selection of a register/memory address. (See this answer and the corresponding question for more detail.)

• Assembly is free for platform etc it can have arg as one register and return its result as a flag... If it has to communicate or to be used to C language programs than yes has to follow API... – user58988 Feb 4 '19 at 22:27

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

# SQL-based languages can output via SELECT or PRINT

We have countless examples of highly upvoted answers sending SQL output to either the results pane (via SELECT) or to the messages pane (via PRINT), depending on which works the best and is shorter for a particular challenge.

Both are acceptable.

(This uses terminology from MS SQL Management Studio, other platforms/clients likely have equivalents.)

Other outputs may be possible as well, such as outputting graphical results to the spatial results pane.

# Image output may consist of a matrix or 2d array for raster images

This is relevant e.g. for challenges. (e.g. for languages that otherwise have no way of writing an image file or opening a window for the graphical display)

# Image output may consist of a pixel-shader

A pixel-shader is a function that takes the (x,y) coordinates of a pixel as an input, and outputs the color of this pixel (e.g. a scalar for grayscale images, a tuple for color images, a boolean value for binary images etc.)

This is relevant e.g. for challenges. (also for GLSL / Shadertoy answers.)

• Isn't this explicitly disallowed for graphical-output at the moment? I suspect it had a good reason too. Same for your other suggestion here. – Mast Nov 25 '19 at 9:56
• @Mast It seems to me that the tag wiki only addresses ASCII-art, but these two types do not seem to be captured, that is why I also thought it would be a good idea to clarify that explicitly. – flawr Nov 25 '19 at 10:15
• I find it weird that this lets solutions not specify the size of the image. Or does the function gave to produce some different value indicating out-of-bounds? – xnor Nov 25 '19 at 10:25
• @xnor The size of the image should be specified, but I think the key is that if you want a bigger resolution of the same image you could just sample (x,y) more finely as an alternative to defining a bigger resolution in the start. – flawr Nov 25 '19 at 10:59
• @flawr I'm not clear how you'd sample more finely. Are (x,y) floats? – xnor Nov 25 '19 at 11:02
• If we normalize the coordinates e.g. to [0,1]^2 then (x,y) could be interpreted as floats, right. If you're using integers then the program itself also takes the limits (e.g. in shadertoy this is implicit) – flawr Nov 25 '19 at 12:39

# Functions may output by calling a callback function or event

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

Example in JavaScript that outputs Hello world!:

f=>f("Hello world!")


This may be shorter than returning a value in some languages:

import random as r
def f(x):
q=r.random()
return(q+q/2)/2


As opposed to:

import random as r
def f(x,g):
q=r.random()
g((q+q/2)/2)

• I don't see why this should be useful, considering you can just return the result. – Esolanging Fruit Feb 11 '17 at 2:27
• Never mind, I see why this might be useful. Upvoted. – Esolanging Fruit Feb 13 '17 at 0:16
• @Challenger5 Please explain why. – haykam Feb 28 '17 at 12:34
• @haykam In a language like Python, something like import os [\n] def f(k):return os.system(k) is longer than import os [\n] def f(k,g):g(os.system(k)) – Esolanging Fruit Feb 28 '17 at 17:51
• @Challenger5, the point of specifying default I/O methods is to allow answers to use idiomatic I/O, not to allow golfing a byte off print. – Peter Taylor Mar 1 '17 at 15:04
• @PeterTaylor Might it be more idiomatic in some languages to use continuation-passing style rather than returning? – Esolanging Fruit Apr 26 '17 at 16:49

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

• -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?) – Esolanging Fruit Nov 26 '17 at 23:39
• Also, Don't allow / ask for different things in languages with different capabilities (related issue). – Esolanging Fruit Nov 26 '17 at 23:45
• @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. – Erik the Outgolfer Dec 10 '17 at 12:14
• @EriktheOutgolfer Fair, but my view is that, to avoid ambiguity and rule-lawyering, we should allow all languages to do this. – Esolanging Fruit Dec 10 '17 at 20:57

# Numerical input and output can be given as a string of digits and vice-versa

As long as the input/output format is consistent, you may take a numerical output such as f(123) as a string f("123") and vice-versa. This will also open up other string methods, such as taking a number as a list of digits as per this consensus.

Note that if different behaviours are expected for numbers with leading zeroes, then you will have to take input as a string instead, since strings with leading zeroes will be mapped to the same number.

• "If may not" ~> "You may not"? – Jonathan Frech Sep 18 '19 at 10:10

# Languages that output on program completion can assume termination even when built to hang for the purpose of providing output.

That is, if a program is built to not a terminate, and requires output while running (say, for an Internal Truth Machine, or Scream Very Loudly, etc.), the program can assume to crash or be manually stopped to verify the output scales appropriately.

The argument for this is that several languages (such as Processing[Java], for example) will not print output unless a certain state is reached (the end of a Draw statement in Processing, per se). This means that programs stuck in While Loops would never provide any output. Languages like GolfScript, though it has a Print function, also doesn't actually Print the output until the program is complete - it holds the print in a buffer until the program eventually stops (for any reason, even interrupting). Likewise, GS prints the stack when it closes for any reason (depending on your interpreter; TIO fails at this) - so filling the stack with infinite As then waiting for a crash would also be within this parameter.

Several languages would otherwise fail infinitely-printing hanging challenges due to the act they wouldn't ever be capable of printing given the assumed infinite time and memory, which is the main argument for allowing this sort of output.

# Functions may take input from the output of a callback function

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

Complementing

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

## Functions are allowed to overwrite global data structures as its output

The premise of this is that your language does not support built-in return values.

For example, Assembly language doesn't have a built-in way of returning values, therefore a global accumulator is permitted as a return value.

In Scratch, there are no functions (only procedures), therefore global variables are allowed as the output of the function.

In stack-based languages, functions take arbitary items from the stack, and modify the stack. The return value is therefore stored onto the stack.

# Cellular automata and 2d languages may output by interfacing with a user-built structure

Many of these languages do not have any other way to produce (non-visual) output. By "interfacing", I mean additional structures being created by the user to interact with the existing code in a way that the program provides only the necessary information (such as some sort of serial representation of the outputted values) in a way that could feasibly be used by the user's program to control another turing-complete automaton (so not in a tiny space in the middle of the program, or in an area that will be overwritten).

# Classes, objects or other entities may have separate methods to read the input and return the result

This includes the constructor and destructor.

• Can the destructor return the result? That seems to be along the same lines as outputting to /dev/null. – Esolanging Fruit May 23 '17 at 3:15
• I don't believe a C++ destructor is allowed to return anything. – Riking Jan 11 '18 at 8:21
• Is it okay if the destruct function output something? – l4m2 Mar 8 '18 at 12:09

# Property setters may take input from the set value

• This could use some elaboration. Why is this not a special case of a function taking input as function arguments? And why would anyone answer a PPCG question with a property setter? – Peter Taylor Mar 23 '17 at 8:55

## 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()+")")

• That Python code is...a curried program. what. – CalculatorFeline Jun 20 '17 at 2:18
• +1 from me. If functions can return other functions, full programs should be able to print other full programs – MilkyWay90 Jun 16 '19 at 18:25

# Cellular automata and 2d languages may take input from code being inserted

Many of these languages do not have any other way to take input. By "code being inserted", I mean additional structures being created by the user to interact with the existing code in a way that provides only the necessary information (such as some sort of serial representation of the inputted values).

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

• This has my vote – TheDoctor Nov 4 '14 at 23:40
• 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. – hmatt1 Nov 5 '14 at 2:12

# Program may take input from environment variables

Maybe... We don't have this discussed yet (?)

# 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!';");

• 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... – ETHproductions Feb 11 '17 at 2:22
• @ETHproductions Rewrote to avoid mentioning that. – jimmy23013 Feb 12 '17 at 2:41

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. • I don't see how this is any different than hardcoding I/O with variables, which is not allowed. – user45941 Dec 5 '17 at 20:35 • @Mego What do you mean? I'm not entirely sure what "hardcoding I/O with variables" refers to. – Nissa Dec 5 '17 at 21:21 • codegolf.meta.stackexchange.com/a/2423/45941 – user45941 Dec 5 '17 at 21:23 • Ah, I see then. – Nissa Dec 5 '17 at 21:26 • 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. – jimmy23013 May 28 '19 at 20:49 # 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';  # 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. • 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". – Kamil Drakari Oct 27 '17 at 19:26 • Then why do we allow the same thing in "assembly" languages? – 12Me21 Oct 27 '17 at 19:53 • 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. – Kamil Drakari Oct 27 '17 at 20:07 • Registers in assembly language are analogous to local vars and temporaries (like a return-value object) in languages like C. Languages that don't have locals or return-values don't have anything like registers. I didn't downvote, though; if the language has a gosub and real-world (non-golf) usage does write "functions" this way, there's certainly an argument to be made. The justification by analogy with asm is pretty weak, though. In asm you need registers just to evaluate expressions, and there are only a small fixed number of them. – Peter Cordes Oct 8 '19 at 18:17 # 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). • Well, cryptographic random values can still be generated in Turing tarpits. – Nissa Dec 5 '17 at 18:29 • @StephenLeppik I was thinking more of completely deterministic languages where you would need the random seed for any subsequent random values. – Οurous Dec 5 '17 at 18:49 • 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. – nimi Dec 6 '17 at 23:03 • @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. – user45941 Dec 12 '17 at 6:19 • @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. ... – nimi Dec 12 '17 at 14:36 • @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). – nimi Dec 12 '17 at 14:36 • @nimi That falls afoul of this loophole. – user45941 Dec 12 '17 at 14:38 • @Mego: True for my mini example. However, the advantage for #2 remains even if the task is more complicated. – nimi Dec 12 '17 at 14:44 # 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). • this seems to be a special case of "functions may use out parameters" – Jasen Dec 22 '16 at 10:55 • but ps doesn't report it after the program ends :( – Jasen Dec 22 '16 at 11:01 # 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.

• Isn't the PHP example actually reading from STDIN? In which case, why shouldn't all of those bytes count? – Martin Ender Nov 3 '14 at 0:09
• 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. – Ismael Miguel Nov 3 '14 at 0:14
• @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? – TheDoctor Nov 4 '14 at 23:42
• @TheDoctor Doesn't matter anyway - no rep gain/loss for meta. – user10766 Nov 6 '14 at 21:44

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

• This essentially makes the submission a snippet instead of a full program, which the other meta question decided should not be a valid default. – Martin Ender Sep 11 '15 at 18:25
• @MartinBüttner Unless the programming language is, of course, a turing machine. (I do vote for placing the tape head at the return value) – yyny Jan 31 '16 at 0:41
• 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) – aloisdg moving to codidact.com Jul 6 '16 at 20:32

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

• -1 (Can't vote) – Nathan Merrill Mar 17 '16 at 21:43
• 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. – djechlin Jun 6 '16 at 18:11
• @djechlin we are intentionally separating input/output methods. Outputting on variables is listed here: meta.codegolf.stackexchange.com/a/6965/20198 – Nathan Merrill Jun 6 '16 at 18:14
• 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. – djechlin Jun 6 '16 at 18:15
• 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. – Nathan Merrill Jun 6 '16 at 18:18
• This is most relevant to the Sinclair ZX80 and ZX81 BASIC. – Shaun Bebbers Apr 4 '17 at 9:04