# 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

# Octave, 4 bytes

xkcd Random Number Generator

@()4


Assigns an anonymous function to the default return variable ans. Call using ans(<parameters>). Any number of input parameters are allowed.

Code length is a random number of bytes.

Sample run on ideone.

# dc, 2

[]


Try it online!

This pushes a dc macro to the stack. I'm not sure if this is satisfies "The function must be assigned to some sort of variable" or not. If not, then it can be stored to a variable for 2 extra bytes:

# dc, 4

[]sf


Try it online!

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

• I like this, BLC is underrated – Mega Man Aug 4 '19 at 19:55

# Attache -R, 2 bytes

{}


Try it online!

An empty lambda. This approach takes advantage of the -R flag, which automatically saves the results of the topmost expression as _1 (also accessible through _). It can be used later, but only by the next expression which does not create a topmost expression. Usually, the next line.

## Attache, 5 bytes

f:={}


Try it online!

A more conventional approach. Saves an empty lambda to the variable f.

## Attache, 6 bytes

f[]:=0


Try it online!

Another conventional approach. Defines a named function f with no arguments, returning 0.

# C#, 10 Bytes

Same as Java.

void f(){}

• Just like the Java answer, I'm pretty sure f=a->a; is allowed. – Kevin Cruijssen Jul 9 '19 at 8:00

# Rust, 6 characters

f=||0;


A closure (also called lambda) that takes no parameters and always returns 0.

(As far as I understand the challenge, only the assignment has to be included, the variable declaration not.)

Try it online!

# Runic Enchantments, 1 byte

B
>00B\$',$$;  Try it online! The second line is in the footer on TIO as all it does is initialize the program and invoke the function, and print the return values thereof (that being a pointer to the next instruction after the return statement--utilizing vs. discarding these values normally distinguishes a function call with a return), and as such should not be included in the bytecount any more than static void main(int[] args) { f(); } would be. Similarly it shows the function "being assigned to a variable" (that being its literal coordinates, 0,0) and "invoked later" at position 3,1. For example, this program invokes the same function after performing some other operations and does so at a different point in the code. Note that the IP does not change direction automatically and may result in unexpected behavior, so functions typically start with a directional command, however in golfing it is insured that the IP is already going in the desired direction, resulting in not needing the extra byte(s). # Abusing language features, 0 bytes  / B̭ >00/',$$;

Try it online!

The first line (blank) is the 0-byte function. The second acts as function call and return. The third line initializes the program and produces output.

I definitely don't think that this should count, but thought I'd at least include it after I realized that I could arrange things this way. If bytes were to be counted from B call to B call in order of program flow, the function contains 8 bytes (four spaces, one /, one B, and two bytes for the ̭ directional modifier).

• I was indeed wondering if there were any other normally verbose language (I posted LUA) that might have something special in place. – ouflak Jul 9 '19 at 8:14
• @ouflak Fair, but doubtful. I'm not even sure I can even really count my 0 byter, as it takes advantage of a 2D language feature and TIO's counting metrics to cheat. – Draco18s no longer trusts SE Jul 9 '19 at 13:25

## Commodore 64 Basic, 13 bytes

1DEFFNI(X)=X


The identity function. This, or any other function with a 1-byte body, is the shortest possible function in C64 Basic.

(Oddly, the empty function is considered a valid function by the C64 interpreter. But since actually calling it generates a runtime syntax error, it fails the "control must return" criterion.)

• Should also work on all variants of Commodore BASIC from BASIC 2 through to BASIC 7 – Shaun Bebbers Jul 10 '19 at 8:15

# Swift, 8 bytes

let f={}


Stores in f a void function with no parameters.

# Inform 7, 10 bytes

To x:stop.


This defines a "phrase" (the simplest form of callable function), which does nothing and returns. It can then be called by name:

When play begins, x.


# Inform 6, 5 bytes

[f;];


The syntax for defining a routine in Inform 6 looks something like this:

[ name arg1 arg2 ;
contents;
return;
];


So [f;]; defines a routine named f which takes no arguments and does nothing.

# Zozotez: 8

(:'q(\))
;; test
(q) ; ==> NIL


Explanation:

• : is set
• 'q is the quoted symbol (the binding) q
• \ is lambda so (\) is short for (\ () ())

# Common Lisp: 11

(defun q())
;; test
(q) ; ==> NIL


# R5RS/R6RS/R7RS Scheme: 12

(define(q)1)
;; test
(q) ; ==> 1


## Clojure, 10 bytes

Returns nil.

(defn f[])


# CJam, 4 bytes

{}:F


Call it with F.

If just leaving a function on the stack is enough, then we can remove 2 bytes:

{}


# Lua, 15 bytes

s=function()end


Try it online!

# C++14, 12 bytes

Empty lambda with no params

auto f=[]{};


# Gema, 4 characters

f:=0


Actually a domain, which is a bit like namespace, but in Gema is the way to define both new pattern arguments and new functions.

(I guess the function's result could be nothing, but on f:= the interpreter segfaults. ☹)

Try it online!

# jq, 8 characters

def f:0;


A function that takes no parameters and always returns 0.

Try it online!

# R + dplyr, 20 bytes

library(dplyr)
.%>%.


Try it at RDRR!

Returns a functional sequence, the identity function.

Much longer than the other R answer but interesting in its own right.

## Pyret, 7 bytes

a=_ + 0


Returns exactly what is passed to it. Underscore paired with an operand implicitly makes a function.

# PowerShell, 0 bytes

Try it online!

You can save this code as a file with .ps1 extension (for example, f.ps1) and call it .\f.ps1. You can directly call this function from another code as scriptblock &{}. The function is inside the brackets.

• Clever. Wish I'd spotted this one myself as I rather like to fiddle around PowerShell. – ouflak Jul 9 '19 at 8:11

# REXX, 0 bytes

A REXX program may call another REXX program as an external function. Given an empty file as above, saved as f.rexx, you may call it from another program as:

call f


Or, if you're unconcerned by the difference between proper functions and subroutines, call it as:

f()


# REXX, 8 bytes

f:return


This is a minimal procedure when called from within the same program. Call using call f or f() if you're not concerned about the philosophical difference between functions and procedures.

# Commodore 64 Basic, 9 bytes

You call the subroutine by GOSUB "\line number" and returns to the caller by RETURN. The function can be accessed anytime by calling the same line number, but the control should always return.

0reT   //Program continues after last Gosub executed
8goS0  //Call subroutine at line number 0


To execute this smallest program type: RUN8.

This program use tokens similarly to Shaun's answer.

Some facts of this program:

• There is no input to the actual function, but the function can access global variables.
• There is no return value from the function.
• The function has no name.
• The function is assigned a 16-bit variable that represents the basic line number (of low and high byte order).

This is the smallest I can think of, but of course expanding the number of digits in the line numbers will acually increase the codesize of the basic program.

For an demonstration with text-output, run this program by typing RUN8:

0 PRINT "INSIDE SUBROUTINE" : RETURN
8 PRINT "CALLING SUBROUTINE" : GOSUB 0
10 PRINT "RETURNED FROM SUBROUTINE"

• This also works: 0 return 1 gosub myfunction run with go to 1 (go to and goto in Commodore BASIC are equivalent) - or to save bytes, try: 0reT 1goS again execute with go to 1 - will not with in C128 BASIC but will on the C64. For testing change line zero to 0?"myfunction":reT – Shaun Bebbers Jul 18 '19 at 11:54
• @ShaunBebbers So I can shave off one byte to my example. Though, would be nice to have several functions than just one, even though the rules say "Write a function". – Natural Number Guy Jul 18 '19 at 14:54
• To summarise, on the C64/VIC-20 if you go to 0 you can simply go to (or goto if you like) as the interpreter will assume zero if no number is entered after the statement. Same principle applies with gosub and run – Shaun Bebbers Jul 18 '19 at 15:01
• No, if I counted the interpreter I could write this in machine code or 6510 asm. I count each petscii char (in basic) as one byte instead. My entry is a basic code program. – Natural Number Guy Jul 18 '19 at 15:08
• Not that this is important (not to me anyway) but according to fre(0) 0reT 1goS uses 14 bytes in all. – Shaun Bebbers Jul 18 '19 at 15:42

# 33, 2 bytes

{}


The name of a function is whatever is in the destination string when the function is defined. Since the string registers are initialised empty when the program starts, this is valid and callable.

## Keg, 4 bytes

@a|@


To call the function:

@a@


This is unimplemented, but it is in the documentation.

## GolfScript, 5 bytes

{}: ;


This assigns a no-op to a space. In order to call the function, type a space.

• I don't think the second example counts because it seems like more of an IIFE - can you access that function to call anywhere in memory, or is it lost after it gets immediately called? – connectyourcharger Jul 21 '19 at 13:53

# Commodore C64 BASIC, 30 BASIC bytes (tokens) used.

 0pO785,210:pO786,255:?usr(0)


This sets the USR BASIC memory pointer to 0xffd2 in the Kernal, which outputs a character on the current device (device 3 being the screen, set to device 4 or 5 and the output is to a printer, and 8 - 15 will be a disk drive, 1 is for tape).

You may therefore write your own machine code function that returns to BASIC if you put the memory location of your routine as above at location 785 (0x0311) as low-byte/high-byte. This must always be 16 bits.

# Pip, 4 bytes

a:{}


Try it online!

# Pip, 0 bytes?

I don't know if this counts but the empty program in Pip is a function. It can be called by (f). If not then the 1 byte program a should count

# Pari/GP, 5 bytes

f()=0


Try it online!

# Agda, 22 chars

Surprisingily difficult because of the required type signature and the lack of a preimported anything.

f : Set → _
f x = Set