Write a function

15

8

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.

connectyourcharger

Posted 2019-07-04T23:15:31.190

Reputation: 2 056

Question was closed 2019-08-11T23:32:09.307

Comments are not for extended discussion; this conversation has been moved to chat.

– James – 2019-07-10T15:09:38.833

3I 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 – 2019-07-17T04:42:49.957

2Also, since when can you vote to reopen your own post?? – Jo King – 2019-07-17T04:52:19.733

7You need to be a lawyer rather than a programmer to compete in this challenge. – anatolyg – 2019-07-21T17:36:19.433

2This should definitely not have been reopened in its current state. – Mego – 2019-08-11T23:32:24.520

Answers

30

x86 / x64 machine code, 1 byte

c3

Assembly:

ret

Try it online! (nasm)

¯\_(ツ)_/¯

negative seven

Posted 2019-07-04T23:15:31.190

Reputation: 1 931

2But this doesn't give the function a name or store a reference to the function in a variable. – Tanner Swett – 2019-07-07T15:05:37.280

4@TannerSwett It can be called by address. – negative seven – 2019-07-07T16:00:51.817

7You can add a label in assembly. It does not increase the size of the compiled code, and gives the function a name. – Daniil Tutubalin – 2019-07-08T22:19:24.617

21

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!

Nick Kennedy

Posted 2019-07-04T23:15:31.190

Reputation: 11 829

The 0 byte answer should work. – lirtosiast – 2019-07-05T08:30:27.330

@KevinCruijssen yes! Good spot – Nick Kennedy – 2019-07-05T11:36:41.720

@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 – 2019-07-05T11:44:14.593

@KevinCruijssen thanks! – Nick Kennedy – 2019-07-05T11:44:36.220

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. – Post Rock Garf Hunter – 2019-07-05T12:48:12.120

@SriotchilismO'Zaic it’s debatable I think - I guess without the newline, the link can only be used to process a command-line argument, and so arguably it’s being used as an anonymous function, but the newline itself isn’t part of the link/function. – Nick Kennedy – 2019-07-05T12:58:46.120

@SriotchilismO'Zaic that'd mean that the Python answer (and many other languages where newlines are important) would need to include a trailing newline too, as def f():1 f() doesn't call the function either – dzaima – 2019-07-05T13:29:42.437

@dzaima Hm, yeah good point. I don't think I am going to comment on all the existing answers though. I think this probably just points to a lack of clarity in the original question. – Post Rock Garf Hunter – 2019-07-05T13:54:19.067

@SriotchilismO'Zaic really the need of a trailing newline for functions where it's needed is applicable to any question that allows a function as an answer, it's definitely not a fault of this specific question – dzaima – 2019-07-05T13:59:45.737

Whole challenge is mostly divided into two parts: create a function and provide a way to call it. Some have way to call it without assignment (like in x86/64 assembly answer because of free jumps), few find a way to take advantage of fact that nothing is still an empty function. When we combine those, we get this… – val says Reinstate Monica – 2019-07-11T13:32:38.440

13

Javascript, 6 bytes

f=_=>0

Includes variable assignment. Not much to see here.

recursive

Posted 2019-07-04T23:15:31.190

Reputation: 8 616

28To make it looking more emoji-like: o=_=>o – Daniil Tutubalin – 2019-07-05T08:13:09.153

3Another emoji d=_=>b – tsh – 2019-07-08T06:26:04.967

3@tsh, alas, b is not defined in this case. – Daniil Tutubalin – 2019-07-08T22:21:04.497

What about f={}? One could argue that a(n object used as a) dictionary is a mathematical function from a set of keys to set of values. – Daniel O – 2019-07-09T02:05:45.307

2@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 – 2019-07-09T02:44:00.463

2@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 – 2019-07-09T03:12:00.437

9

Python 3, 9 bytes

def f():1

Try it online!

Dat

Posted 2019-07-04T23:15:31.190

Reputation: 879

8

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

Neil

Posted 2019-07-04T23:15:31.190

Reputation: 95 035

8

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.

Robin Ryder

Posted 2019-07-04T23:15:31.190

Reputation: 6 625

1function()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 – 2019-07-08T17:25:59.067

1@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 – 2019-07-08T21:05:55.697

4function()0 would be assigned to .Last.value() though that would be pushing it – OganM – 2019-07-08T21:23:27.633

@OganM Nice point! – Robin Ryder – 2019-07-09T05:46:29.810

pryr::f(x) if we allow pryr. – qwr – 2019-11-14T21:53:39.737

8

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.

Tanner Swett

Posted 2019-07-04T23:15:31.190

Reputation: 531

clever, type level functions – Mega Man – 2019-08-04T19:52:42.880

6

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

Jo King

Posted 2019-07-04T23:15:31.190

Reputation: 38 234

6

C (gcc), 5 bytes

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

f(){}

Try it online!

ErikF

Posted 2019-07-04T23:15:31.190

Reputation: 2 149

1"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 – 2019-07-08T23:51:25.643

6

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!

a stone arachnid

Posted 2019-07-04T23:15:31.190

Reputation: 1 053

6

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

Lukas Lang

Posted 2019-07-04T23:15:31.190

Reputation: 231

1Hey, it can be called, and the return value is optional. This counts. – connectyourcharger – 2019-07-07T09:24:29.940

1If 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 – 2019-07-07T17:10:51.627

@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... – Lukas Lang – 2019-07-07T17:42:36.903

5

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.

mbomb007

Posted 2019-07-04T23:15:31.190

Reputation: 21 944

5

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

val says Reinstate Monica

Posted 2019-07-04T23:15:31.190

Reputation: 409

5

POSIX sh, 6 bytes

s()(1)

Using curly braces requires one more character.

kojiro

Posted 2019-07-04T23:15:31.190

Reputation: 717

5

Java, 10 Bytes

this should match the rules of the challenge

void f(){}

pixma140

Posted 2019-07-04T23:15:31.190

Reputation: 135

1I'm pretty sure f=a->a; is valid as well. :) – Kevin Cruijssen – 2019-07-09T07:58:39.790

@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 – 2019-07-09T13:02:11.747

2

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 – 2019-07-09T13:16:21.497

4

Perl 5, 7 bytes

sub f{}

Try it online!

Xcali

Posted 2019-07-04T23:15:31.190

Reputation: 7 671

4

Kotlin, 8 bytes

val f={}

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

Peïo THIBAULT

Posted 2019-07-04T23:15:31.190

Reputation: 41

4

shortC, 1 byte

A

Try it online!

Transpiles into this C:

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

a stone arachnid

Posted 2019-07-04T23:15:31.190

Reputation: 1 053

4

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.

ceilingcat

Posted 2019-07-04T23:15:31.190

Reputation: 5 503

3

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 _ {}

SmileAndNod

Posted 2019-07-04T23:15:31.190

Reputation: 119

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 – 2019-07-06T17:57:09.007

@mbomb007 I see your point, and fixed it accordingly – SmileAndNod – 2019-07-06T19:19:43.367

3

Ruby, 6 bytes

Proc called f which accepts no argument and returns nil.

f=->{}

Try it online!

Eric Duminil

Posted 2019-07-04T23:15:31.190

Reputation: 701

3

Haskell, 5 bytes

f x=0

Try it online!

Joseph Sible-Reinstate Monica

Posted 2019-07-04T23:15:31.190

Reputation: 556

3

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.

Silvio Mayolo

Posted 2019-07-04T23:15:31.190

Reputation: 1 817

3

F# (.NET Core), 9 bytes

let f a=a

Try it online!

LSM07

Posted 2019-07-04T23:15:31.190

Reputation: 191

3

Python 3, 10 bytes

f=lambda:0

Try it online!

U10-Forward

Posted 2019-07-04T23:15:31.190

Reputation: 347

Can't you just do def f():0 to save a byte? – EdgyNerd – 2019-07-24T09:28:44.120

@EdgyNerd Dat answered before me with that code, and i don't want to copy it – U10-Forward – 2019-07-24T09:58:13.877

Oh ok, never mind then – EdgyNerd – 2019-07-24T12:20:40.260

3

Pascal 23bytes

procedure A;begin end;

rnso

Posted 2019-07-04T23:15:31.190

Reputation: 1 635

2

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!

manatwork

Posted 2019-07-04T23:15:31.190

Reputation: 17 865

2

AppleScript, 10

on a()
end

Explained, compiled, and including invocation:

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

-- invoke it:
a()

a stone arachnid

Posted 2019-07-04T23:15:31.190

Reputation: 1 053

2

Japt, 2 bytes

_

Called as $U ($.

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

Try it

Embodiment of Ignorance

Posted 2019-07-04T23:15:31.190

Reputation: 7 014

2

SmileBASIC (>=3), 9 bytes

DEF A
END

Function is called by A.

snail_

Posted 2019-07-04T23:15:31.190

Reputation: 1 982

Alternatively DEF A*END in SB4 – 12Me21 – 2019-07-08T15:00:55.887

2

Wolfram Language (Mathematica), 2 bytes

#&

Try it online!

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

Roman

Posted 2019-07-04T23:15:31.190

Reputation: 1 190

You need to save the function to a variable according to the rules, e.g. f=#& – Lukas Lang – 2019-07-07T09:01:49.557

@LukasLang it's automatically saved to %1 so there's no need for an explicit assignment to a variable like f. – Roman – 2019-07-07T09:06:18.430

Good point, that should be enough to fulfil the rules – Lukas Lang – 2019-07-07T09:07:13.350

2

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.

beaker

Posted 2019-07-04T23:15:31.190

Reputation: 2 349

2

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

Draco18s no longer trusts SE

Posted 2019-07-04T23:15:31.190

Reputation: 3 053

I was indeed wondering if there were any other normally verbose language (I posted LUA) that might have something special in place. – ouflak – 2019-07-09T08:14:19.147

@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 – 2019-07-09T13:25:45.557

2

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!

Digital Trauma

Posted 2019-07-04T23:15:31.190

Reputation: 64 644

2

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.

Fyr

Posted 2019-07-04T23:15:31.190

Reputation: 561

I like this, BLC is underrated – Mega Man – 2019-08-04T19:55:59.343

2

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.

Conor O'Brien

Posted 2019-07-04T23:15:31.190

Reputation: 36 228

2

C#, 10 Bytes

Same as Java.

void f(){}

Michael

Posted 2019-07-04T23:15:31.190

Reputation: 121

Just like the Java answer, I'm pretty sure f=a->a; is allowed. – Kevin Cruijssen – 2019-07-09T08:00:12.027

2

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

Mark

Posted 2019-07-04T23:15:31.190

Reputation: 2 099

Should also work on all variants of Commodore BASIC from BASIC 2 through to BASIC 7 – Shaun Bebbers – 2019-07-10T08:15:58.407

2

Swift, 8 bytes

let f={}

Stores in f a void function with no parameters.

idrougge

Posted 2019-07-04T23:15:31.190

Reputation: 641

1

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.

Draconis

Posted 2019-07-04T23:15:31.190

Reputation: 561

1

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.

Draconis

Posted 2019-07-04T23:15:31.190

Reputation: 561

1

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

Sylwester

Posted 2019-07-04T23:15:31.190

Reputation: 3 678

1

Clojure, 10 bytes

Returns nil.

(defn f[])

NikoNyrh

Posted 2019-07-04T23:15:31.190

Reputation: 2 361

1

CJam, 4 bytes

{}:F

Call it with F.

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

{}

Esolanging Fruit

Posted 2019-07-04T23:15:31.190

Reputation: 13 542

1

Lua, 15 bytes

s=function()end

Try it online!

ouflak

Posted 2019-07-04T23:15:31.190

Reputation: 925

1

C++14, 12 bytes

Empty lambda with no params

auto f=[]{};

rep_movsd

Posted 2019-07-04T23:15:31.190

Reputation: 141

1

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!

manatwork

Posted 2019-07-04T23:15:31.190

Reputation: 17 865

1

jq, 8 characters

def f:0;

A function that takes no parameters and always returns 0.

Try it online!

manatwork

Posted 2019-07-04T23:15:31.190

Reputation: 17 865

1

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.

Giuseppe

Posted 2019-07-04T23:15:31.190

Reputation: 21 077

1

Pyret, 7 bytes

a=_ + 0

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

MLavrentyev

Posted 2019-07-04T23:15:31.190

Reputation: 181

1

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.

mazzy

Posted 2019-07-04T23:15:31.190

Reputation: 4 832

2Clever. Wish I'd spotted this one myself as I rather like to fiddle around PowerShell. – ouflak – 2019-07-09T08:11:59.510

1

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.

idrougge

Posted 2019-07-04T23:15:31.190

Reputation: 641

1

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.

Shaun Bebbers

Posted 2019-07-04T23:15:31.190

Reputation: 1 814

1

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"

Natural Number Guy

Posted 2019-07-04T23:15:31.190

Reputation: 211

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 – 2019-07-18T11:54:04.457

1@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 – 2019-07-18T14:54:15.130

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 – 2019-07-18T15:01:15.507

yes, it will save one PETSCII character if that's what you are counting. If you want to know how many bytes used by the interpreter, use the fre(0) function – Shaun Bebbers – 2019-07-18T15:02:21.320

1No, 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 – 2019-07-18T15:08:46.567

1Not that this is important (not to me anyway) but according to fre(0) 0reT 1goS uses 14 bytes in all. – Shaun Bebbers – 2019-07-18T15:42:00.533

1

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.

TheOnlyMrCat

Posted 2019-07-04T23:15:31.190

Reputation: 1 079

1

GolfScript, 5 bytes

{}: ;

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

user85052

Posted 2019-07-04T23:15:31.190

Reputation:

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 – 2019-07-21T13:53:35.037

1

Keg, 4 bytes

@a|@

To call the function:

@a@

This is unimplemented, but it is in the documentation.

user85052

Posted 2019-07-04T23:15:31.190

Reputation:

1

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

Kenneth Taylor

Posted 2019-07-04T23:15:31.190

Reputation: 183

0

Pari/GP, 5 bytes

f()=0

Try it online!

alephalpha

Posted 2019-07-04T23:15:31.190

Reputation: 23 988

0

Agda, 22 chars

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

f : Set → _
f x = Set

Mega Man

Posted 2019-07-04T23:15:31.190

Reputation: 1 379

0

BFASM / asm2bf, 9 bytes

lbl 1
ret

It builds to following brainfuck code (omitting boilerplate):

]>+<<+<<[>>->+<<<-]>>>[<<<+>>>-]<[->+<<[>>>-<<+<-]>[<+>-]>>[<->[-]]<[<<<+>>>-
]<]>>[-]<<<<[>>+>+<<<-]>>[<<+>>-]>[[-]>>>>>><<<<<<<<<[-]>[-]>>>>>>>>>>>>>>>[-
]>[>>]<<->[<<<[<<]>+>[>>]>-]<<<[<<]>[<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>-]<<<<<<<
[-]<<<<<<]<<<[>>+>+<<<-]>>[<<+>>-]>[[-]

Krzysztof Szewczyk

Posted 2019-07-04T23:15:31.190

Reputation: 3 819

0

Zsh, 5 bytes

Anonymous function... Try it online!

()(1)

roblogic

Posted 2019-07-04T23:15:31.190

Reputation: 554