# What even is a “function” by our standards?

On a recent question, the following was brought up by @Sp3000:

In fact, I'm almost starting to wonder what a "function" is, since it seems like every language with goto could define a "function" the same way...

What are the most basic restrictions for what can be defined as a "function", an allowed answer type, that separates it from a "snippet", which is not allowed?

• Related. – user62131 Feb 17 '17 at 17:22

A function is a piece of reusable code that will produce consistent output(s) given consistent input(s). Note that consistent doesn't necessarily mean identical; it merely means that the function won't do something unexpected (i.e. randint(1, 4) will always return a random integer n such that 1 <= n <= 4, and never an integer outside of that range, a floating point value, or the lyrics to a song).

Additionally, there must be some consistent method of providing input to the function (like function arguments, putting input on the stack, or saving input in specified registers), a consistent method of retrieving the output (like return values, modified parameters, stack contents, or register contents), and a consistent method of calling the function.

For languages that have support for functions/methods/procedures, their built-in functions are acceptable so long as they meet these requirements. Additionally, for any language (including languages without such constructs such as purely imperative languages), if there is a way to write a piece of code that adheres to the above constraints, it is acceptable.

As for writing a submission that uses a function, the result of evaluating the submission code should either result in a named function being defined, or an unnamed function being defined that can be assigned a variable reference, and can subsequently called.

Examples of acceptable submissions (not an exhaustive list):

• Macro functions (C/C++: #define ISEVEN(X) (X % 2 == 0))
• Named and unnamed lambda functions (Python: lambda x: x % 2 == 0)
• Functions with helper functions also defined (Python: f = lambda x: x == 0; lambda x: f(x % 2))
• I'm not a fan of this definition, because it limits languages that have functions. Aka, " if there is a way to write a piece of code that adheres to the above constraints, it is acceptable" should be true of any language, not just ones that don't have a thing called a "function/method/procedure". – Nathan Merrill Jun 10 '16 at 13:19
• @NathanMerrill I absolutely agree. I have edited the answer. – Mego Jun 11 '16 at 19:50
• Honestly this feels much too lenient and rather describing the validity of what is a valid function through its call. It doesn't really address the aspect of aliasability and beyond the "if your language already has functions..." thing it seems that even code snippets will make the cut. – Downgoat Jan 4 '17 at 6:43
• @Downgoat Aliasing is not a good requirement to have - it completely disallows tacit functions (like in J or Jelly). – Mego Jan 4 '17 at 6:45
• @Mego by aliasing I'm referring to the idea that the "function" can be used without repeating the entire functions source. – Downgoat Jan 4 '17 at 10:18
• @Downgoat I'm not convinced that's a necessary requirement. I'm sure an example exists where having that restriction would disallow an otherwise-valid language. My goals in writing these requirements were to allow everything we currently allow, disallow everything we currently forbid, and provide room for more esoteric constructs to be allowed so long as they comply with the base set of requirements. – Mego Jan 4 '17 at 11:10
• @Mego this would also allow for +1 to be a valid function in JS to increment a number if it didn't have functions natively – Downgoat Jan 4 '17 at 11:23
• @Downgoat No it wouldn't. Functions still have to abide by the acceptable I/O methods. – Mego Jan 4 '17 at 11:36
• @Mego considering I/O is your significant factor in determining whether or not it's a function, you haven't exhaustively, not even provided bounds for what fits these constraints that is specific to being considered a function. – Downgoat Jan 4 '17 at 12:08
• @Downgoat That's because we already have a post for acceptable I/O methods. – Mego Jan 4 '17 at 21:08
• @Mego this is for what defines a function, unless every valid full program or code segment in esolangs is a valid "function" – Downgoat Jan 5 '17 at 1:23
• @Downgoat So what? Disallow STDIN and STDOUT for functions? Obviously esolangs are going to blur the line between functions and programs, but that isn't the point. We're not trying to draw that line - we're trying to draw the line between what is acceptable and what isn't. – Mego Jan 5 '17 at 3:45
• @Mego we are trying to define a function across a broad range of languages, the question is whether certain constructs can qualify as a function. You've basically defined function as anything doing I/O, you haven't provided any criteria which specifies whether it's a function or not except I/O, which also is specified for every other form of submission making it redundant when considering whether or not a construct is a function. Your ideas of reusability are vague and undefined while being arguably additional important criteria in the validity of being a function. – Downgoat Jan 5 '17 at 4:45
• What does "consistent input(s)" mean when invoked with non-trivial expressions? E.g. the example macro will give different results for ISEVEN(2) vs ISEVEN(6-4), and IMO that's not acceptable. – Peter Taylor Dec 21 '17 at 8:58
• @PeterTaylor Consistent input(s) means that the same input(s) will result in the same output(s) (factoring in “hidden” inputs like global and internal state, such as for PRNGs). The example C macro I gave was poorly-written. – Mego Dec 21 '17 at 16:23

# A function is an independent routine which can perform I/O in some form.

It is a section of code, which can be directly inserted into a program with no modification, and should be able to be assigned/named/referred to in some way.

It should be able to run independent of surrounding code.

It should be able to take input/output in some form.

The function should be able to perform a successive chain of operations. Meaning these "functions" should be able to somehow be used in a program to fulfill the definition of one. A function only capable of doing a single binary operation wouldn't be valid unless, recursion or some other way for a successive operation to be performed exists

A functions call should not require calling any other functions or code besides defining the input. A function should receive the input in a format that's is compliant with the challenges rules

A function should have a way for he input to be directly passed to it (it doesn't have to take input in that way, just the functionality should be available)

That said, if it's blatantly obvious it's not a function, it's not a function.

Also as @Mego said:

Python has functions. Anything else is not a function. The further explanations in the answer are intended to be used for languages that don't have a cut-and-dry definition of a function

We know what a function is. It's futile calling what's not a function a function, it's very difficult to come up with a perfectly objective set of rules describing what a function is, but we all know what a function is, and what it is not.

If the language formally defines it to be a function/method/lambda, it's a function. this doesn't mean an esolang creator abusing their ability to define a function, is exempt from rules and other restrictions applied upon programs and functions.

In your question you asked if gotos could be considered functions. IIRC, they can't have I/O passed directly through them, so no.

• A goto could read from STDIN and print to STDOUT. – Dennis Mar 19 '16 at 16:13
• @Dennis But I seem to recall the default being that functions can't read from STDIN... – SuperJedi224 Mar 20 '16 at 18:55
• – Dennis Mar 20 '16 at 20:26
• @Dennis Oh, okay. Thanks. – SuperJedi224 Mar 20 '16 at 20:29
• Does this mean print input()*2 is a Python 2 function to double a number? e: reread the answer, it doesn't meet the first requirement. – undergroundmonorail Mar 21 '16 at 0:11
• Wait, hang on. What about "print input()*2", i.e. a string of code? It can be named, it does i/o, and can be called independent of surrounding code. Of course, if it's named f it has to be called as exec f but I don't know how that's fundamentally different than f(). What about g="input()*2";eval(g)? Is that different because eval is a function? – undergroundmonorail Mar 21 '16 at 0:16
• What about "*2" as a function to double a number? Takes input as a string. Here's the syntax for this style of function with arguments: eval(argument+code), e.g. h="*2";eval("4"+h) – undergroundmonorail Mar 21 '16 at 0:18
• @undergroundmonorail not everything can be defined completely objectively, but in cases like that where it's dead obvious its not a function, I don't see the point of even defining rules why that's invalid. That said I've added additional rules. Also h isn't directly inserted into the code. It is placed in a string violating the first rule which I should clarify – Downgoat Mar 21 '16 at 1:04
• @undergroundmonorail Python has functions. Anything else is not a function. The further explanations in the answer are intended to be used for languages that don't have a cut-and-dry definition of a function (if I'm understanding it correctly). – Mego Mar 21 '16 at 16:29
• Actually, I've thought some more about this (because of this meta post), and I don't agree with my previous comment anymore - anything is valid so long as it meets the bare requirements (reusability and consistency). – Mego Jun 6 '16 at 8:44

Here is my definition of a function:

1. A function must have the ability to be called by its name, object, reference, or other handle (this prevents code snippets). The only exception to this is the main method (or equivalent) of a program.
2. A function must be able to take data as input by accepting parameters and output data by returning values. If side-effects occur, the function shouldn't exploit them; any parameters should be arguments, and any results should use your language's equivalent of return.
3. Multiple functions can be counted as one function, provided that each helper function meets the 2 previous conditions.

Lastly, as @Downgoat's answer says, if something is obviously not a function, then it shouldn't be treated as such.

A full program is any code that can be compiled, interpreted, or otherwise executed in a non-REPL environment without any modification. Note that some code can be classified as both functions and full programs.

Anything that is not either a function or a full program is a code snippet.

For an example, let's look at Java lambda expressions.

1. You can assign a lambda expression to an object, so it passes the handle test.
2. It is possible to write a lambda expression that accepts parameters and returns values.

For example:

(int a, double b)->{
return 4;
}


Since a lambda that satisfies #2 exists, any lambda is considered a function, even those that don't accept parameters:

()->{
System.out.println("Lambda is running");
}

1. N/A

I made a quick flowchart to categorize various bits of code (sorry for my lack of flowcharting skills):

• My only two jibes about this that doesn't quite align with how I view/define functions: scope and compilability/runnability by itself. Some languages don't have function-per-function scope, and some languages are flexible enough to allow functions to run on their own. (main(String[] args) is a function, too) – Addison Crump Mar 22 '16 at 19:10
• I'll add an exception for main methods, but if the function has no scope, what separates it from a goto statement? – Daniel M. Mar 22 '16 at 19:12
• Why are goto statements different from functions? They're reusable, they apply an operation... – Addison Crump Mar 22 '16 at 19:15
• They are unable to accept and return variables without referencing anything outside of the scope of the goto. It is pretty hard to do so, considering that they don't have any scope. – Daniel M. Mar 22 '16 at 19:17
• This definition doesn't allow for helper functions to be defined, which is allowed by our current policy on function submissions. – Mego Mar 22 '16 at 19:23
• @Mego I'm not familiar with python helper functions. What part does this definition not work with? – Daniel M. Mar 22 '16 at 19:27
• It has nothing to do with Python specifically. By helper functions, I mean code that defines more than one function. A Python example would be f = lambda x: x >= 0; lambda x: f(x) and f(-x). f is a helper function here, and the last (unnamed) lambda is the submission function (the one that is to be run for the purpose of getting a result). – Mego Mar 22 '16 at 19:34
• @Mego Modified the definition; see point 5 – Daniel M. Mar 22 '16 at 19:39
• I simplified the rules a bit without really changing the meaning (flowchart updated accordingly). Feel free to rollback if you think its too different. – Daniel M. Mar 22 '16 at 20:15
• For the "does it have it's own scope", this would elimate JavaScrit's arrow functions. As they do not hae their own scope – Downgoat Mar 22 '16 at 22:27
• @Downgoat ...but JavaScript does have scope: w3schools.com/js/js_scope.asp – Daniel M. Mar 22 '16 at 22:29
• @DanielM. those are not arrow functions. Arrow functions are define with => and do not have their own scope. – Downgoat Mar 22 '16 at 22:29
• @Downgoat Maybe I can combine 2 and 3 and change it to >A function must be able to accept and return data without using any variables that are not part of the function, and without any side-effects. – Daniel M. Mar 22 '16 at 22:35
• @DanielM. Side effects are important in non-point-free languages. – Mego Mar 23 '16 at 2:50
• C macros also do not have scope, so your definition excludes them. – MegaTom Jun 6 '16 at 17:28

# A function returns back to where it came from

A function is has two parts: An arbitrary length block of code which is run when the function is "called", and a "calling" statement.

After the block of code has run, the code pointer must automatically return back to the statement that made the call.

The key word is automatically. A GOTO at the end of a code block is not automatic.

• How is a goto at the end of a block not acceptable, but a return statement is? – Mego Mar 22 '16 at 6:34
• What if there was a variable that contained a pointer to the last place in the code that was GOTO'd from. Then you could just say GOTO lastgoto... Oh, wait. That's what RETURN does with GOSUB. – wizzwizz4 Mar 24 '16 at 19:10