11
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ Related. \$\endgroup\$
    – user62131
    Commented Feb 17, 2017 at 17:22

4 Answers 4

8
\$\begingroup\$

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.

\$\endgroup\$
10
  • 4
    \$\begingroup\$ A goto could read from STDIN and print to STDOUT. \$\endgroup\$
    – Dennis Mod
    Commented Mar 19, 2016 at 16:13
  • \$\begingroup\$ @Dennis But I seem to recall the default being that functions can't read from STDIN... \$\endgroup\$ Commented Mar 20, 2016 at 18:55
  • 2
    \$\begingroup\$ @SuperJedi224 Functions may take input via the same methods as full programs \$\endgroup\$
    – Dennis Mod
    Commented Mar 20, 2016 at 20:26
  • \$\begingroup\$ @Dennis Oh, okay. Thanks. \$\endgroup\$ Commented Mar 20, 2016 at 20:29
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 21, 2016 at 0:11
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 21, 2016 at 0:16
  • \$\begingroup\$ 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) \$\endgroup\$ Commented Mar 21, 2016 at 0:18
  • \$\begingroup\$ @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 \$\endgroup\$
    – Downgoat
    Commented Mar 21, 2016 at 1:04
  • 2
    \$\begingroup\$ @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). \$\endgroup\$
    – user45941
    Commented Mar 21, 2016 at 16:29
  • 2
    \$\begingroup\$ 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). \$\endgroup\$
    – user45941
    Commented Jun 6, 2016 at 8:44
6
\$\begingroup\$

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))
\$\endgroup\$
15
  • \$\begingroup\$ 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". \$\endgroup\$ Commented Jun 10, 2016 at 13:19
  • \$\begingroup\$ @NathanMerrill I absolutely agree. I have edited the answer. \$\endgroup\$
    – user45941
    Commented Jun 11, 2016 at 19:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – Downgoat
    Commented Jan 4, 2017 at 6:43
  • \$\begingroup\$ @Downgoat Aliasing is not a good requirement to have - it completely disallows tacit functions (like in J or Jelly). \$\endgroup\$
    – user45941
    Commented Jan 4, 2017 at 6:45
  • \$\begingroup\$ @Mego by aliasing I'm referring to the idea that the "function" can be used without repeating the entire functions source. \$\endgroup\$
    – Downgoat
    Commented Jan 4, 2017 at 10:18
  • \$\begingroup\$ @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. \$\endgroup\$
    – user45941
    Commented Jan 4, 2017 at 11:10
  • \$\begingroup\$ @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 \$\endgroup\$
    – Downgoat
    Commented Jan 4, 2017 at 11:23
  • \$\begingroup\$ @Downgoat No it wouldn't. Functions still have to abide by the acceptable I/O methods. \$\endgroup\$
    – user45941
    Commented Jan 4, 2017 at 11:36
  • \$\begingroup\$ @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. \$\endgroup\$
    – Downgoat
    Commented Jan 4, 2017 at 12:08
  • \$\begingroup\$ @Downgoat That's because we already have a post for acceptable I/O methods. \$\endgroup\$
    – user45941
    Commented Jan 4, 2017 at 21:08
  • \$\begingroup\$ @Mego this is for what defines a function, unless every valid full program or code segment in esolangs is a valid "function" \$\endgroup\$
    – Downgoat
    Commented Jan 5, 2017 at 1:23
  • \$\begingroup\$ @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. \$\endgroup\$
    – user45941
    Commented Jan 5, 2017 at 3:45
  • \$\begingroup\$ @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. \$\endgroup\$
    – Downgoat
    Commented Jan 5, 2017 at 4:45
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 21, 2017 at 8:58
  • \$\begingroup\$ @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. \$\endgroup\$
    – user45941
    Commented Dec 21, 2017 at 16:23
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ How is a goto at the end of a block not acceptable, but a return statement is? \$\endgroup\$
    – user45941
    Commented Mar 22, 2016 at 6:34
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – wizzwizz4
    Commented Mar 24, 2016 at 19:10
0
\$\begingroup\$

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

A flowchart

\$\endgroup\$
16
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ Commented Mar 22, 2016 at 19:10
  • \$\begingroup\$ I'll add an exception for main methods, but if the function has no scope, what separates it from a goto statement? \$\endgroup\$
    – Daniel M.
    Commented Mar 22, 2016 at 19:12
  • \$\begingroup\$ Why are goto statements different from functions? They're reusable, they apply an operation... \$\endgroup\$ Commented Mar 22, 2016 at 19:15
  • \$\begingroup\$ 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. \$\endgroup\$
    – Daniel M.
    Commented Mar 22, 2016 at 19:17
  • \$\begingroup\$ This definition doesn't allow for helper functions to be defined, which is allowed by our current policy on function submissions. \$\endgroup\$
    – user45941
    Commented Mar 22, 2016 at 19:23
  • \$\begingroup\$ @Mego I'm not familiar with python helper functions. What part does this definition not work with? \$\endgroup\$
    – Daniel M.
    Commented Mar 22, 2016 at 19:27
  • \$\begingroup\$ 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). \$\endgroup\$
    – user45941
    Commented Mar 22, 2016 at 19:34
  • \$\begingroup\$ @Mego Modified the definition; see point 5 \$\endgroup\$
    – Daniel M.
    Commented Mar 22, 2016 at 19:39
  • \$\begingroup\$ I simplified the rules a bit without really changing the meaning (flowchart updated accordingly). Feel free to rollback if you think its too different. \$\endgroup\$
    – Daniel M.
    Commented Mar 22, 2016 at 20:15
  • \$\begingroup\$ For the "does it have it's own scope", this would elimate JavaScrit's arrow functions. As they do not hae their own scope \$\endgroup\$
    – Downgoat
    Commented Mar 22, 2016 at 22:27
  • \$\begingroup\$ @Downgoat ...but JavaScript does have scope: w3schools.com/js/js_scope.asp \$\endgroup\$
    – Daniel M.
    Commented Mar 22, 2016 at 22:29
  • \$\begingroup\$ @DanielM. those are not arrow functions. Arrow functions are define with => and do not have their own scope. \$\endgroup\$
    – Downgoat
    Commented Mar 22, 2016 at 22:29
  • \$\begingroup\$ @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. \$\endgroup\$
    – Daniel M.
    Commented Mar 22, 2016 at 22:35
  • \$\begingroup\$ @DanielM. Side effects are important in non-point-free languages. \$\endgroup\$
    – user45941
    Commented Mar 23, 2016 at 2:50
  • 1
    \$\begingroup\$ C macros also do not have scope, so your definition excludes them. \$\endgroup\$
    – MegaTom
    Commented Jun 6, 2016 at 17:28

You must log in to answer this question.

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