# Write a function [closed]

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.

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

# x86 / x64 machine code, 1 byte

c3


Assembly:

ret


Try it online! (nasm)

¯\_(ツ)_/¯

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

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

• The 0 byte answer should work. – lirtosiast Jul 5 '19 at 8:30
• @KevinCruijssen yes! Good spot – Nick Kennedy Jul 5 '19 at 11:36
• @NickKennedy You can use <pre><code>...</code></pre> to preserve leading/trailing spaces/newlines in code-blocks. I've edited your answer accordingly. :) – Kevin Cruijssen Jul 5 '19 at 11:44
• @KevinCruijssen thanks! – Nick Kennedy Jul 5 '19 at 11:44
• 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. – Wheat Wizard Jul 5 '19 at 12:48

# Javascript, 6 bytes

f=_=>0


Includes variable assignment. Not much to see here.

• To make it looking more emoji-like: o=_=>o – Daniil Tutubalin Jul 5 '19 at 8:13
• Another emoji d=_=>b – tsh Jul 8 '19 at 6:26
• @tsh, alas, b is not defined in this case. – Daniil Tutubalin Jul 8 '19 at 22:21
• @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. – recursive Jul 9 '19 at 2:44
• @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. – tsh Jul 9 '19 at 3:12

# Python 3, 9 bytes

def f():1


Try it online!

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


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.

• clever, type level functions – Mega Man Aug 4 '19 at 19:52

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

• 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 – Giuseppe Jul 8 '19 at 17:25
• @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. – Robin Ryder Jul 8 '19 at 21:05
• function()0 would be assigned to .Last.value() though that would be pushing it – OganM Jul 8 '19 at 21:23
• @OganM Nice point! – Robin Ryder Jul 9 '19 at 5:46
• pryr::f(x) if we allow pryr. – qwr Nov 14 '19 at 21:53

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

# C (gcc), 5 bytes

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

f(){}


Try it online!

• "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. – val says Reinstate Monica Jul 8 '19 at 23:51

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

# [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[]

• Hey, it can be called, and the return value is optional. This counts. – connectyourcharger Jul 7 '19 at 9:24
• 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. – Roman Jul 7 '19 at 17:10
• @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 Globalf, 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... – Lukas Lang Jul 7 '19 at 17:42

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

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

# POSIX sh, 6 bytes

s()(1)


Using curly braces requires one more character.

# Java, 10 Bytes

this should match the rules of the challenge

void f(){}

• I'm pretty sure f=a->a; is valid as well. :) – Kevin Cruijssen Jul 9 '19 at 7:58
• @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? – pixma140 Jul 9 '19 at 13:02
• 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. – Kevin Cruijssen Jul 9 '19 at 13:16

# Perl 5, 7 bytes

sub f{}


Try it online!

# Kotlin, 8 bytes

val f={}

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

# shortC, 1 byte

A


Try it online!

Transpiles into this C:

 int main(int argc, char **argv){;}


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

f x=0


Try it online!

# Tcl, 65 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 _ {}

• 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. – mbomb007 Jul 6 '19 at 17:57
• @mbomb007 I see your point, and fixed it accordingly – vo1stv Jul 6 '19 at 19:19

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

# F# (.NET Core), 9 bytes

let f a=a


Try it online!

# Ruby, 6 bytes

Proc called f which accepts no argument and returns nil.

f=->{}


Try it online!

# Python 3, 10 bytes

f=lambda:0


Try it online!

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

# Pascal 23bytes

procedure A;begin end;


## AppleScript, 10

on a()
end


Explained, compiled, and including invocation:

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

-- invoke it:
a()


# Japt, 2 bytes

_


Called as $U ($.

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

Try it

# SmileBASIC (>=3), 9 bytes

DEF A
END


Function is called by A.

• Alternatively DEF A*END in SB4 – 12Me21 Jul 8 '19 at 15:00

# Wolfram Language (Mathematica), 2 bytes

#&


Try it online!

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

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