19
\$\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\$
5
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – DJMcMayhem
    Commented Jul 10, 2019 at 15:09
  • 4
    \$\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
    Commented Jul 17, 2019 at 4:42
  • 2
    \$\begingroup\$ Also, since when can you vote to reopen your own post?? \$\endgroup\$
    – Jo King
    Commented Jul 17, 2019 at 4:52
  • 9
    \$\begingroup\$ You need to be a lawyer rather than a programmer to compete in this challenge. \$\endgroup\$
    – anatolyg
    Commented Jul 21, 2019 at 17:36
  • 2
    \$\begingroup\$ This should definitely not have been reopened in its current state. \$\endgroup\$
    – user45941
    Commented Aug 11, 2019 at 23:32

62 Answers 62

31
\$\begingroup\$

x86 / x64 machine code, 1 byte

c3

Assembly:

ret

Try it online! (nasm)

¯\_(ツ)_/¯

\$\endgroup\$
3
  • 2
    \$\begingroup\$ But this doesn't give the function a name or store a reference to the function in a variable. \$\endgroup\$ Commented Jul 7, 2019 at 15:05
  • 4
    \$\begingroup\$ @TannerSwett It can be called by address. \$\endgroup\$ Commented Jul 7, 2019 at 16:00
  • 9
    \$\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\$ Commented Jul 8, 2019 at 22:19
22
\$\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\$
10
  • \$\begingroup\$ The 0 byte answer should work. \$\endgroup\$
    – lirtosiast
    Commented Jul 5, 2019 at 8:30
  • \$\begingroup\$ @KevinCruijssen yes! Good spot \$\endgroup\$ Commented Jul 5, 2019 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\$ Commented Jul 5, 2019 at 11:44
  • \$\begingroup\$ @KevinCruijssen thanks! \$\endgroup\$ Commented Jul 5, 2019 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
    Commented Jul 5, 2019 at 12:48
14
\$\begingroup\$

Javascript, 6 bytes

f=_=>0

Includes variable assignment. Not much to see here.

\$\endgroup\$
6
  • 31
    \$\begingroup\$ To make it looking more emoji-like: o=_=>o \$\endgroup\$ Commented Jul 5, 2019 at 8:13
  • 4
    \$\begingroup\$ Another emoji d=_=>b \$\endgroup\$
    – tsh
    Commented Jul 8, 2019 at 6:26
  • 3
    \$\begingroup\$ @tsh, alas, b is not defined in this case. \$\endgroup\$ Commented Jul 8, 2019 at 22:21
  • 4
    \$\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
    Commented Jul 9, 2019 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
    Commented Jul 9, 2019 at 3:12
10
\$\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\$
1
  • \$\begingroup\$ clever, type level functions \$\endgroup\$
    – univalence
    Commented Aug 4, 2019 at 19:52
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\$

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\$
5
  • 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
    Commented Jul 8, 2019 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\$ Commented Jul 8, 2019 at 21:05
  • 4
    \$\begingroup\$ function()0 would be assigned to .Last.value() though that would be pushing it \$\endgroup\$
    – OganM
    Commented Jul 8, 2019 at 21:23
  • \$\begingroup\$ @OganM Nice point! \$\endgroup\$ Commented Jul 9, 2019 at 5:46
  • \$\begingroup\$ pryr::f(x) if we allow pryr. \$\endgroup\$
    – qwr
    Commented Nov 14, 2019 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
  • 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\$ Commented Jul 8, 2019 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\$
3
  • 1
    \$\begingroup\$ Hey, it can be called, and the return value is optional. This counts. \$\endgroup\$
    – moltarze
    Commented Jul 7, 2019 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
    Commented Jul 7, 2019 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
    Commented Jul 7, 2019 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\$
3
  • 1
    \$\begingroup\$ I'm pretty sure f=a->a; is valid as well. :) \$\endgroup\$ Commented Jul 9, 2019 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
    Commented Jul 9, 2019 at 13:02
  • 3
    \$\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\$ Commented Jul 9, 2019 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\$
4
\$\begingroup\$

Pascal, 22 bytes

procedure A;begin end;
\$\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\$
2
  • \$\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
    Commented Jul 6, 2019 at 17:57
  • \$\begingroup\$ @mbomb007 I see your point, and fixed it accordingly \$\endgroup\$
    – vo1stv
    Commented Jul 6, 2019 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\$

Binary lambda calculus, 1 byte

0100_0010

In lambda calculus: (λf. _)(λx. x)

The underscore denotes where the rest of the program should go. The function can be accessed through its De Bruijn index.

\$\endgroup\$
1
  • \$\begingroup\$ I like this, BLC is underrated \$\endgroup\$
    – univalence
    Commented Aug 4, 2019 at 19:55
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\$
3
  • \$\begingroup\$ Can't you just do def f():0 to save a byte? \$\endgroup\$
    – EdgyNerd
    Commented Jul 24, 2019 at 9:28
  • \$\begingroup\$ @EdgyNerd Dat answered before me with that code, and i don't want to copy it \$\endgroup\$ Commented Jul 24, 2019 at 9:58
  • \$\begingroup\$ Oh ok, never mind then \$\endgroup\$
    – EdgyNerd
    Commented Jul 24, 2019 at 12:20
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\$
1
  • 1
    \$\begingroup\$ Alternatively DEF A*END in SB4 \$\endgroup\$
    – 12Me21
    Commented Jul 8, 2019 at 15:00

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