15
\$\begingroup\$

I honestly can't believe this challenge does not already exist.

The challenge

Write a function.

The specifics

  • Your program must define some sort of callable function. This includes anything commonly known as a function, a lambda function, or a subroutine. All of these types of callables will be referred to as a "function" in this post.

    1. Input to the function is optional and not required.

    2. A return value from the function is also optional and not required but control must return to the calling program.

  • The function must be assigned to some sort of variable so that it is possible to be accessed at a later time. This includes indirect assignment (in most common languages where declaring a named function automatically adds the name into the current scope) and direct assignment (assigning an anonymous function to a variable directly).

  • The function does not need to be named.

  • The function must be created by you - you cannot just assign a default function from the language to a variable.

  • None of the standard loopholes, please.

This is , so lowest score in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – DJMcMayhem Jul 10 at 15:09
  • 3
    \$\begingroup\$ I don't get why this was reopened when none of the reasons it was closed in the first place have been addressed, just hidden away by mods in a chat \$\endgroup\$ – Jo King Jul 17 at 4:42
  • 2
    \$\begingroup\$ Also, since when can you vote to reopen your own post?? \$\endgroup\$ – Jo King Jul 17 at 4:52
  • 7
    \$\begingroup\$ You need to be a lawyer rather than a programmer to compete in this challenge. \$\endgroup\$ – anatolyg Jul 21 at 17:36
  • 2
    \$\begingroup\$ This should definitely not have been reopened in its current state. \$\endgroup\$ – Mego Aug 11 at 23:32

62 Answers 62

30
\$\begingroup\$

x86 / x64 machine code, 1 byte

c3

Assembly:

ret

Try it online! (nasm)

¯\_(ツ)_/¯

\$\endgroup\$
  • 2
    \$\begingroup\$ But this doesn't give the function a name or store a reference to the function in a variable. \$\endgroup\$ – Tanner Swett Jul 7 at 15:05
  • 4
    \$\begingroup\$ @TannerSwett It can be called by address. \$\endgroup\$ – negative seven Jul 7 at 16:00
  • 7
    \$\begingroup\$ You can add a label in assembly. It does not increase the size of the compiled code, and gives the function a name. \$\endgroup\$ – Daniil Tutubalin Jul 8 at 22:19
21
\$\begingroup\$

Jelly, 0 bytes

Try it online!

A monadic link that returns its argument. Since it is the first function to appear in the script, it can be called using 1Ŀ.

Thanks to @lirtosiast for pointing out that a 0 byte link/function would work in Jelly.

I.e.


3,4,5 1Ŀ

Try it online!

\$\endgroup\$
  • \$\begingroup\$ The 0 byte answer should work. \$\endgroup\$ – lirtosiast Jul 5 at 8:30
  • \$\begingroup\$ @KevinCruijssen yes! Good spot \$\endgroup\$ – Nick Kennedy Jul 5 at 11:36
  • \$\begingroup\$ @NickKennedy You can use <pre><code>...</code></pre> to preserve leading/trailing spaces/newlines in code-blocks. I've edited your answer accordingly. :) \$\endgroup\$ – Kevin Cruijssen Jul 5 at 11:44
  • \$\begingroup\$ @KevinCruijssen thanks! \$\endgroup\$ – Nick Kennedy Jul 5 at 11:44
  • \$\begingroup\$ My knowledge of jelly is pretty thin but don't you need a newline to end the link? It seems to be used in the example. \$\endgroup\$ – Wheat Wizard Jul 5 at 12:48
13
\$\begingroup\$

Javascript, 6 bytes

f=_=>0

Includes variable assignment. Not much to see here.

\$\endgroup\$
  • 28
    \$\begingroup\$ To make it looking more emoji-like: o=_=>o \$\endgroup\$ – Daniil Tutubalin Jul 5 at 8:13
  • 3
    \$\begingroup\$ Another emoji d=_=>b \$\endgroup\$ – tsh Jul 8 at 6:26
  • 3
    \$\begingroup\$ @tsh, alas, b is not defined in this case. \$\endgroup\$ – Daniil Tutubalin Jul 8 at 22:21
  • 2
    \$\begingroup\$ @DanielO: One could certainly argue that. But in my opinion, one could even more effectively argue that you can't call it, so it's not a function. In javascript, a function call is unambiguously represented with parentheses. \$\endgroup\$ – recursive Jul 9 at 2:44
  • 2
    \$\begingroup\$ @DaniilTutubalin But this only matters if you invoke it. And there is no such requirement about the function whether should run without throwing an exception. \$\endgroup\$ – tsh Jul 9 at 3:12
9
\$\begingroup\$

Python 3, 9 bytes

def f():1

Try it online!

\$\endgroup\$
8
\$\begingroup\$

ZX Spectrum BASIC, 6 bytes

DEF FN f()=PI

Hex dump: CE 66 28 29 3D A7. CE is a 1-byte keyword for DEF FN (including the trailing space), while A7 is a 1-byte keyword for PI. Call using FN f(). Example program:

  10 PRINT FN f(): DEF FN f()=PI

Output:

3.1415927
\$\endgroup\$
8
\$\begingroup\$

Haskell, 3 bytes

o=9

This code defines a polymorphic function called o which takes one type parameter and one typeclass instance parameter. When this function is called, it takes the given typeclass instance, gets its fromInteger member, calls that member with the Integer value for 9, and returns the result.

Granted, what I just described is merely the behavior of the Haskell function 9, and my code merely defines a function called o which is equivalent to 9.

Now the only question is, is the 9 function "created by you," or is it "a default function from the language"?

I think that it is "created by you." My reason for saying this is that if you read the specification for Haskell, you will (I assume) find no mention of a 9 function anywhere. Instead, the specification states that you can create a number literal by stringing together one or more digits. Therefore, by writing a string of digits, I have written a function—even if I just so happen to have only used one digit.

\$\endgroup\$
  • \$\begingroup\$ clever, type level functions \$\endgroup\$ – Mega Man Aug 4 at 19:52
8
\$\begingroup\$

R, 9 bytes

body(t)=0

Try it online!

I think this complies with the rules. The function t takes no input and outputs 0. This works because there already exists a function called t (the transposition function) and it redefines the body of the function; it would not work with say body(a)=0 (no object called a) or body(F)=0 (F is a logical, not a function). I think it complies because it is still created by me: I am not reusing what the pre-defined function does, simply its name.

I don't think I've ever seen this used by R golfers, but there may be situations where it allows us to save a few bytes on challenges where we need a helper function.

A more standard solution would have been:

R, 13 bytes

f=function()0

Try it online!

Function which takes no input and outputs 0. This is 1 byte shorter than the function which takes no input and outputs nothing, which would be

f=function(){}

If we try to define a function with no body (f=function()), R interprets this as an incomplete command (this might not be true in older versions of R).

As pointed out by OganM, we take this down to 11 bytes with

R, 11 bytes

function()0

Try it online!

which technically complies with the challenge requirement that the function be assigned to some sort of variable, since it is (ephemerally) assigned to .Last.value.

\$\endgroup\$
  • 1
    \$\begingroup\$ function()0 should work for your second answer since the function need not be named. Neat trick on body<-, I've tried to use body and the like to do some of the weird challenges to mess with the language \$\endgroup\$ – Giuseppe Jul 8 at 17:25
  • 1
    \$\begingroup\$ @Giuseppe "The function must be assigned to some sort of variable", so I don't think function()0 complies of the rules of this challenge. I'd be happy to give a bounty to an answer which uses the body()=" trick successfully. \$\endgroup\$ – Robin Ryder Jul 8 at 21:05
  • 4
    \$\begingroup\$ function()0 would be assigned to .Last.value() though that would be pushing it \$\endgroup\$ – OganM Jul 8 at 21:23
  • \$\begingroup\$ @OganM Nice point! \$\endgroup\$ – Robin Ryder Jul 9 at 5:46
  • \$\begingroup\$ pryr::f(x) if we allow pryr. \$\endgroup\$ – qwr Nov 14 at 21:53
6
\$\begingroup\$

Perl 6, 5 bytes

$!=!*

Try it online!

Creates a Whatever lambda that returns the boolean not of its parameter, and assigns it to the variable $!.

\$\endgroup\$
6
\$\begingroup\$

C (gcc), 5 bytes

Defines a function f that takes no arguments and technically returns an undefined integer value.

f(){}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ "takes no arguments" — lies badly. That's one of common misunderstandings about C: empty argument list means undefined amount of undefined type arguments without portable way to access them. As a sidenote, this is also C/C++ incompatibility. \$\endgroup\$ – val says Reinstate Monica Jul 8 at 23:51
6
\$\begingroup\$

Whitespace, 7 bytes


  

	

Creates a subroutine that returns control to the caller.

Explained in context:

[N
S S N
_Create_Label][N
T   N
_Return]

Try it online!

\$\endgroup\$
6
\$\begingroup\$

[Wolfram Language (Mathematica)], 1 byte

This one is slightly questionable:

f

Defines f, which can be "called" e.g. by f[], which "returns" the expression f[]

\$\endgroup\$
  • 1
    \$\begingroup\$ Hey, it can be called, and the return value is optional. This counts. \$\endgroup\$ – connectyourcharger Jul 7 at 9:24
  • 1
    \$\begingroup\$ If this solution is acceptable, then the zero-byte answer is just as good: your "definition" of f doesn't do anything (apart from remembering "I've seen f") and can be left out. You can call f[] nonetheless, still returning unevaluated f[]. However, in any case you're mostly playing tricks with the pattern-replacer and not instructing to evaluate a function. \$\endgroup\$ – Roman Jul 7 at 17:10
  • \$\begingroup\$ @Roman I've considered adding the zero byte version, but in the end I felt like that's even more questionable: this actually creates the symbol Global`f, while the empty version doesn't do that (you could argue that Null is assigned to %1, but Null is a built-in "function"). But as I've noted in the answer, whether the one byte solution is valid is also not entirely clear... \$\endgroup\$ – Lukas Lang Jul 7 at 17:42
5
\$\begingroup\$

Forth (gforth), 5 bytes

This is a function named f that does nothing.

: f ;

Try it Online

In the TIO code, I added a footer of see f, which prints the definition of the function.

\$\endgroup\$
5
\$\begingroup\$

Lua, 8 bytes

f=load''

Try it online!

Defines a (global) function f.

This uses Lua load function to compile given string which happens to be empty in our case (empty code is valid code) into function which does exactly what we wrote in its body: nothing.

For ones wondering, standard solution would be

function f()end

but this is longer (15 bytes).

\$\endgroup\$
5
\$\begingroup\$

POSIX sh, 6 bytes

s()(1)

Using curly braces requires one more character.

\$\endgroup\$
5
\$\begingroup\$

Java, 10 Bytes

this should match the rules of the challenge

void f(){}
\$\endgroup\$
  • 1
    \$\begingroup\$ I'm pretty sure f=a->a; is valid as well. :) \$\endgroup\$ – Kevin Cruijssen Jul 9 at 7:58
  • \$\begingroup\$ @Kevin Cruijssen I am no Java expert and I never used the Java array notation. How could I make your solution getting compiled? I initially "tested" my method in this TIO and then appended your approach there. Now, the compiler expects an identifier. Any explanation or wrong usage by me? \$\endgroup\$ – pixma140 Jul 9 at 13:02
  • 2
    \$\begingroup\$ It's a Java 8+ lambda function. So either of these two would work in this case. Here a more in depth explanation of Java 8+ lambdas in case you aren't familiar with them yet. \$\endgroup\$ – Kevin Cruijssen Jul 9 at 13:16
4
\$\begingroup\$

Perl 5, 7 bytes

sub f{}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Kotlin, 8 bytes

val f={}

An empty function stored in a variable f.
Call it using f() or f.invoke().

\$\endgroup\$
4
\$\begingroup\$

shortC, 1 byte

A

Try it online!

Transpiles into this C:

 int main(int argc, char **argv){;}
\$\endgroup\$
4
\$\begingroup\$

C (gcc), 14 13 bytes

(*f)()=L"Ã";

Try it online!

This defines a function f returning int and accepting an unspecified number (and type) of parameters, the machine code of which is contained within the string literal. The unicode character à (stored in memory as 0xc3 0x00 0x00 0x00 on a little endian machine) corresponds to the x86 ret instruction that returns from the function. Non x86 architectures may require different opcode(s) to return.

gcc may require the -zexecstack flag to avoid a segfault.

\$\endgroup\$
3
\$\begingroup\$

Haskell, 5 bytes

f x=0

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Tcl, 6 5 11 bytes

set f {_ ;}

Try it online!

Including the assignment to the variable f as part of the bytecount to comply with rules. With this change, the more conventional definition below ties the one above for bytecount:

proc f _ {}
\$\endgroup\$
  • \$\begingroup\$ Is this a named function? The function must be assigned to some sort of variable so that it is possible to be accessed at a later time. \$\endgroup\$ – mbomb007 Jul 6 at 17:57
  • \$\begingroup\$ @mbomb007 I see your point, and fixed it accordingly \$\endgroup\$ – SmileAndNod Jul 6 at 19:19
3
\$\begingroup\$

XSLT, 134 bytes

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template name="a"></xsl:template></xsl:stylesheet>

A template is the closest thing this language has to a function. It can definitely be called; it takes zero arguments and "returns" the empty string.

\$\endgroup\$
3
\$\begingroup\$

F# (.NET Core), 9 bytes

let f a=a

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Ruby, 6 bytes

Proc called f which accepts no argument and returns nil.

f=->{}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 3, 10 bytes

f=lambda:0

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Can't you just do def f():0 to save a byte? \$\endgroup\$ – EdgyNerd Jul 24 at 9:28
  • \$\begingroup\$ @EdgyNerd Dat answered before me with that code, and i don't want to copy it \$\endgroup\$ – U10-Forward Jul 24 at 9:58
  • \$\begingroup\$ Oh ok, never mind then \$\endgroup\$ – EdgyNerd Jul 24 at 12:20
3
\$\begingroup\$

Pascal 23bytes

procedure A;begin end;
\$\endgroup\$
2
\$\begingroup\$

AppleScript, 10

on a()
end

Explained, compiled, and including invocation:

on a()    -- declare event handler "a"
end a     -- end declaration

-- invoke it:
a()
\$\endgroup\$
2
\$\begingroup\$

Japt, 2 bytes

_

Called as $U ($.

_ can be replaced with @, Ï, or È.

Try it

\$\endgroup\$
2
\$\begingroup\$

SmileBASIC (>=3), 9 bytes

DEF A
END

Function is called by A.

\$\endgroup\$
  • \$\begingroup\$ Alternatively DEF A*END in SB4 \$\endgroup\$ – 12Me21 Jul 8 at 15:00
2
\$\begingroup\$

Wolfram Language (Mathematica), 2 bytes

#&

Try it online!

Unfortunately, just & does not work (an anonymous function that does nothing).

\$\endgroup\$
  • \$\begingroup\$ You need to save the function to a variable according to the rules, e.g. f=#& \$\endgroup\$ – Lukas Lang Jul 7 at 9:01
  • \$\begingroup\$ @LukasLang it's automatically saved to %1 so there's no need for an explicit assignment to a variable like f. \$\endgroup\$ – Roman Jul 7 at 9:06
  • \$\begingroup\$ Good point, that should be enough to fulfil the rules \$\endgroup\$ – Lukas Lang Jul 7 at 9:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.