25
votes
\$\begingroup\$

The Task

Create a function/subroutine which returns 1. You can make it as elaborate as you like, as long as it returns 1.

The Rules

The entry with the most upvote wins - just like any popularity contest. Good luck!

\$\endgroup\$
8
  • 6
    \$\begingroup\$ +1 four downvotes but 13 Answers?, if people are having fun with this question why so many downvotes? \$\endgroup\$
    – jsedano
    Commented Jul 30, 2013 at 22:48
  • \$\begingroup\$ 13 answers, but only two of them have garnered any votes. Perhaps this is our version of the emacs vs vi question -- one where everyone has an answer but none of them are particularly better than another. \$\endgroup\$
    – breadbox
    Commented Jul 31, 2013 at 19:16
  • 5
    \$\begingroup\$ @anakata, because four (make that six now) people think that this is the kind of question they think would have been better not posted. Some people are against popularity-contest on principle, and this is scraping the bottom of that category. \$\endgroup\$ Commented Aug 1, 2013 at 10:55
  • 1
    \$\begingroup\$ This is one of those places where codegolf fits uneasily into the stackexchange format. Compare the Collatz Conjecture, which is also trending right now. The answers are all pretty mundane (no offense), because it's not a good problem for creative golfing — the naive approach is also the shortest. Whereas in this question, the popularity-contest allows all kinds of interesting answers to a very trivial task. Far more enjoyable to read — but stackexchange is supposed to avoid open-ended stuff like this. Thus the downvotes. \$\endgroup\$
    – breadbox
    Commented Aug 1, 2013 at 17:13
  • \$\begingroup\$ @breadbox Point taken - I will make my challenges more interesting from now :) \$\endgroup\$
    – Doorknob
    Commented Aug 6, 2013 at 2:12

70 Answers 70

1 2
3
0
votes
\$\begingroup\$

Perl

sub add  # I'm very bad at math, I think 1 is the additive identity.
{
    my ($a, $b) = @_;
    if ($a==1)
        {return $b;}
    if ($b==1)
        {return $a;}
    return $a+$b;
}
print(&add(1,1));# prints 1+1:)

https://eval.in/81256 `

\$\endgroup\$
0
votes
\$\begingroup\$

Javascript

function returnOne(){return Math.log(Math.log(Math.log(Math.log(Math.round((Math.log(!![].join()^{}-({}=={})|(0x00|0x11111)-(0x111111&0x10111))/Math.log(2))/(Math.tan(Math.PI/4)*Math.tan(1.48765509)))+(0xFFFF))/Math.log(2))/Math.log(2))/Math.log(2))/Math.log(2)}

\$\endgroup\$
0
votes
\$\begingroup\$

TI-BASIC

:Disp 1&.0&(1
\$\endgroup\$
0
votes
\$\begingroup\$

C

#include <setjmp.h>

int main(int argc, char **argv) {
    jmp_buf env;
    return setjmp(env) || (longjmp(env, 0), 0);
}
\$\endgroup\$
0
votes
\$\begingroup\$

C++

return true;

I don't know if this really counts, but when you print true in C++, it prints 1.

\$\endgroup\$
0
votes
\$\begingroup\$

Zozotez

(: '1 (\ 1 '1))

In Zozotez 1 is just a symbol so here I set 1 to be the anonymous function that has a list argument called 1 which is not used since I'm returning the symbol 1. You can run it like this in a REPL:

(1 '2 '3 (p 'hello)) ; ==> 1 (prints hello)

A full program needs to be one expression so you wrap it in a lambda:

;; using set (:)
((\ ()
   (: '1 (\ 1 '1))
   (p (1 '2 '3 (p 'hello))))))

;; using bind
((\ (1)       
   (p (1 '2 '3 (p 'hello))))
 (\ 1 '1))

Eg. (using bind)

%> echo "((\ (1) (p (1 '2 '3 (p 'hello)))) (\ 1 '1))" | bf zozotez.bf
hello
1

In scheme it's the same as this:

(define I (lambda I '1)) ; switch 1 with I some places to make it legal
(I 2 3 (display 'hello)) ; ==> 1 (prints hello)
\$\endgroup\$
0
votes
\$\begingroup\$

C

Not sure how it works, but it seems legit.

long long int g(unsigned int i, unsigned int *j){
    if (!j) { j = &i; i++; }
    return i?(&i-j) + g(i-1,&i):0;
}

long long int one(unsigned int i) {
    i|=1;
    return g(i,NULL)/g(1,NULL)/i;
}
\$\endgroup\$
0
votes
\$\begingroup\$

C 209

Euler's formula

#include <stdio.h>
#include <math.h>

int e(char i, double pi)
{
    float z = ((cos(pi)) + (i)* sin(pi));
    return (int)z;
}

int main()
{
    char i = 'i';
    double pi = 2*acos(0);
    return -1 * e(i, pi);
}
\$\endgroup\$
0
votes
\$\begingroup\$

C#

public static int return_1()
{
    Random rng = new Random(new Random(12345).Next());
    int x = rng.Next();
    while (x < 1 || x > 1)
    {
        x = calc(((z, y) => z - y), x, rng.Next());
    }
    return x;
}

public static int calc(Func<int, int, int> f, int x, int y)
{
    return f(x, y);
}

If you wait long enough it will return 1 ;)

\$\endgroup\$
0
votes
\$\begingroup\$

Scheme (functional answer)

Real coders are functional programmers; thus real coders never use integers; they rather use functions instead. Fortunately Alonzo Church gave a functional construction of integers. See here or here. Thus you have to define one as:

(define one
   (lambda (f) (lambda (x) (f x))))

And then you can return one.

\$\endgroup\$
1 2
3

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