Sejujurnya aku tidak percaya tantangan ini belum ada.
Tantangan
Tulis fungsi.
Spesifikasinya
Program Anda harus mendefinisikan semacam fungsi yang bisa dipanggil. Ini termasuk apa pun yang umumnya dikenal sebagai fungsi, fungsi lambda, atau subrutin. Semua jenis callable ini akan disebut sebagai "fungsi" dalam posting ini.
Input ke fungsi adalah opsional dan tidak diperlukan.
Nilai kembali dari fungsi juga opsional dan tidak diperlukan tetapi kontrol harus kembali ke program panggilan.
Fungsi tersebut harus ditetapkan ke beberapa jenis variabel sehingga memungkinkan untuk diakses di lain waktu. Ini termasuk penugasan tidak langsung (dalam sebagian besar bahasa yang umum di mana mendeklarasikan fungsi bernama secara otomatis menambahkan nama ke dalam cakupan saat ini) dan penugasan langsung (menugaskan fungsi anonim ke variabel secara langsung).
Fungsi tidak perlu disebutkan namanya.
Fungsi harus dibuat oleh Anda - Anda tidak bisa hanya menetapkan fungsi default dari bahasa ke variabel.
Tidak ada celah standar , silakan.
Ini adalah kode-golf , jadi skor terendah dalam byte menang.
Jawaban:
kode mesin x86 / x64, 1 byte
Majelis:
Cobalah online! (nasm)
¯ \ _ (ツ) _ / ¯
sumber
Jelly , 0 byte
Cobalah online!
Tautan monadik yang mengembalikan argumennya. Karena ini adalah fungsi pertama yang muncul dalam skrip, ia dapat dipanggil menggunakan
1Ŀ
.Terima kasih kepada @ lirtosiast untuk menunjukkan bahwa tautan / fungsi 0 byte akan berfungsi di Jelly.
Yaitu
Cobalah online!
sumber
<pre><code>...</code></pre>
untuk menjaga spasi / baris utama / baris baru di blok kode. Saya telah mengedit jawaban Anda sesuai dengan itu. :)Javascript, 6 byte
Termasuk tugas variabel. Tidak banyak yang bisa dilihat di sini.
sumber
o=_=>o
d=_=>b
b
is not defined in this case.Python 3, 9 bytes
Try it online!
sumber
ZX Spectrum BASIC, 6 bytes
Hex dump:
CE 66 28 29 3D A7
.CE
is a 1-byte keyword forDEF FN
(including the trailing space), whileA7
is a 1-byte keyword forPI
. Call usingFN f()
. Example program:Output:
sumber
Haskell, 3 bytes
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 itsfromInteger
member, calls that member with theInteger
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 calledo
which is equivalent to9
.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.sumber
R, 9 bytes
Try it online!
I think this complies with the rules. The function
t
takes no input and outputs0
. This works because there already exists a function calledt
(the transposition function) and it redefines the body of the function; it would not work with saybody(a)=0
(no object calleda
) orbody(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
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 beIf 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
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
.sumber
function()0
should work for your second answer since the function need not be named. Neat trick onbody<-
, I've tried to usebody
and the like to do some of the weird challenges to mess with the languagefunction()0
complies of the rules of this challenge. I'd be happy to give a bounty to an answer which uses thebody()="
trick successfully.function()0
would be assigned to.Last.value()
though that would be pushing itpryr::f(x)
if we allow pryr.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
$!
.sumber
C (gcc), 5 bytes
Defines a function
f
that takes no arguments and technically returns an undefined integer value.Try it online!
sumber
Whitespace, 7 bytes
Creates a subroutine that returns control to the caller.
Explained in context:
Try it online!
sumber
[Wolfram Language (Mathematica)], 1 byte
This one is slightly questionable:
Defines
f
, which can be "called" e.g. byf[]
, which "returns" the expressionf[]
sumber
f
doesn't do anything (apart from remembering "I've seenf
") and can be left out. You can callf[]
nonetheless, still returning unevaluatedf[]
. However, in any case you're mostly playing tricks with the pattern-replacer and not instructing to evaluate a function.Global`f
, while the empty version doesn't do that (you could argue thatNull
is assigned to%1
, butNull
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...Forth (gforth), 5 bytes
This is a function named
f
that does nothing.Try it Online
In the TIO code, I added a footer of
see f
, which prints the definition of the function.sumber
Lua, 8 bytes
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
but this is longer (15 bytes).
sumber
POSIX sh, 6 bytes
Using curly braces requires one more character.
sumber
Java, 10 Bytes
this should match the rules of the challenge
sumber
f=a->a;
is valid as well. :)Perl 5, 7 bytes
Try it online!
sumber
Kotlin, 8 bytes
An empty function stored in a variable f.
Call it using
f()
orf.invoke()
.sumber
shortC, 1 byte
Try it online!
Transpiles into this C:
sumber
C (gcc),
1413 bytesTry it online!
This defines a function
f
returningint
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 as0xc3 0x00 0x00 0x00
on a little endian machine) corresponds to the x86ret
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.sumber
Haskell, 5 bytes
Try it online!
sumber
Tcl,
6511 bytesTry 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:
sumber
XSLT, 134 bytes
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.
sumber
F# (.NET Core), 9 bytes
Try it online!
sumber
Ruby, 6 bytes
Proc called
f
which accepts no argument and returnsnil
.Try it online!
sumber
Python 3, 10 bytes
Try it online!
sumber
def f():0
to save a byte?Pascal 23bytes
sumber
AppleScript, 10
Explained, compiled, and including invocation:
sumber
Japt, 2 bytes
Called as
$U ($
._
can be replaced with@
,Ï
, orÈ
.Try it
sumber
SmileBASIC (>=3), 9 bytes
Function is called by
A
.sumber
DEF A*END
in SB4Wolfram Language (Mathematica), 2 bytes
Try it online!
Unfortunately, just
&
does not work (an anonymous function that does nothing).sumber
f=#&
%1
so there's no need for an explicit assignment to a variable likef
.