# Add two numbers

Input: Two integers. Preferably decimal integers, but other forms of numbers can be used. These can be given to the code in standard input, as arguments to the program or function, or as a list.

Output: Their sum. Use the same format for output integers as input integers. For example, the input 5 16 would lead to the output 21.

Restrictions: No standard loopholes please. This is , answer in lowest amount of bytes wins.

Notes: This should be fairly trivial, however I'm interested to see how it can be implemented. The answer can be a complete program or a function, but please identify which one it is.

Test cases:

1 2 -> 3
14 15 -> 29
7 9 -> 16
-1 8 -> 7
8 -9 -> -1
-8 -9 -> -17

Or as CSV:

a,b,c
1,2,3
14,15,29
7,9,16
-1,8,7
8,-9,-1
-8,-9,-17

• This is quite trivial, but not really simpler than, e.g., the Hello World catalog. Given that the ability to add integers is one of our two requirements for programming languages, I'd say it's worthwhile to have if properly specified. – Dennis Jul 2 '16 at 0:48
• Can the answer take input with preceding zeros as default? e.g. 5 16 is inputted as 005 016 – FinW Dec 4 '16 at 11:56
• @FinW Sure. As long as they don't get interpreted as octal. – anna328p Dec 4 '16 at 20:47

## Prelude, 4 bytes

??+!

Requires my modified Prelude interpreter which uses decimal I/O.

# Befunge - 5 Bytes

&&+.@

& - Request int from user and push to stack

+ - Pop top two elements from stack and add and push result

. - Pop value and output as int

@ - End program

Try it here (Although you will have to copy/paste into the text area)

Edit: oops, didn't notice the earlier (identical) befunge answer, I'll leave this here unless I'm told to delete it, not sure of the opinion on that.

# GO, 29 bytes

func (a,b int)int{return a+b}

Not that much to say

# Yup, 6 bytes

0*-*-#

Try it online!

## Explanation

0*-*-#
0        push 0 to the stack   [0]
*       place input           [0 a]
-      subtract              [-a]
*     place input           [-a b]
-    subtract              [-a-b]
= [b+a]
#   output

As a function on -cheat mode, 9 bytes:

0~--0~-=+

Try it online!

## SML, 3 bytes

op+

This is the prefix version of the infix +. If we input it like this in an interpreter (for example Moscow ML), it's type is displayed

- op+;
> val it = fn : int * int -> int

which tells us how to use it: Given a tuple of integers, an integer will be returned.

- op+(17,25);
> val it = 42 : int

## GAP, 2 Bytes

\+

The backslash is GAP's way to turn an infix operator (and some more, there is also  for indexing) to a function.

Here is an example use:

gap> \+(4,3);
7

# jq, 3 characters

Sample run:

bash-4.3$jq 'add' <<< '[5,16]' 21 On-line test # Elixir, 7 bytes & &1+&2 Anonymous function defined using the capture operator. Another version is &(&1+&2), however this approach saves 1 byte. The verbose version is fn a,b->a+b end - 15 bytes. Full program with test case (yes, the . in the function call is mandatory!): s=& &1+&2 IO.puts s.(1,6) #7 Try it online on ElixirPlayground ! ## C, 49 bytes main(a,b){scanf("%d %d",&a,&b);printf("%d",a+b);} Can't really do much to golf it down. ### For fun: The following works only if 0 <= sum < 256 (it's 59 bytes long): a;main(c,v)char**v;{return a++&2?c-3:main(c+atoi(*++v),v);} Use gcc to compile, and ./a.out [your 2 nums]; echo$? to run it.

Here's the ungolfed version of that program:

/* Global int auto-initialized to 0 */
a;

/* The main method with two int params */
/* 'c' is argc, and 'v' is argv */
main(c, v)

/* Yes, this is valid. It defines 'v' as a char** */
char** v;

{
/* Checks to see if a == 2, and increments a.
* I only want recursion to happen twice. */
if (a++ & 2)
{
/* Since "argc" is 3, we need to subtract it from the final answer */
return c - 3;
}
else
{
/* Get the next int value of "argv" and add it to 'c'.
* Then call main() again with the updated value */
return main(c + atoi(*++v), v);
}
}

So, main() returns the value of the sum of the two numbers (essentially, I'm forcing it to return a custom exit code). The program won't output the exit code, so calling echo $?, in the same command as running the program, outputs the return value of that program. The range of exit codes only exist between 0 and 255, so if you run the program trying to sum 255 and 1, it will wrap around and output 0. ## Ruby, 17 bytes ** Edit: Big thank you to @steenbergh and @TùxCräftîñg for their help ** def c(a,b)a+b end • Edited the answer. – BoeNoe Oct 20 '16 at 17:37 • @manatwork Edited. – BoeNoe Oct 21 '16 at 11:21 • @manatwork What do you mean? – BoeNoe Oct 21 '16 at 11:24 • @manatwork Oh, ok. – BoeNoe Oct 21 '16 at 11:27 • @manatwork Edited. – BoeNoe Oct 21 '16 at 11:27 # Befunge, 7 bytes #v&< + . @ Befunge is an esoteric language that can execute in any cardinal direction on a 2 dimensional plane. Executing left to right from the top left (default) we get the following set of operations ((x,y) = coordinates of operation with (0,0) in the top left) (0,0): skip next cell in execution path (1,0) (2,0): ask for user input and push it to the stack (3,0): reverse direction of execution (2,0): ask for user input and push it to the stack (1,0): begin executing downward (1,1): pop first two values on stack, add them and push result (1,2): pop stack and output value (1,3): end &&+.@ would also work, and in only 5 bytes, but is not nearly as cool • Invalid, you did not count the newline nor the spacing. Also, this is code golf, the aim is conciesness, not coolness – ASCII-only Jan 29 '19 at 7:53 # Swift, 1 byte + since operator + is defined as function(public func +(lhs: Int, rhs: Int) -> Int) # C, 66 bytes Typical C omissions with regard to declarations, header files, and return statements. This actually works for any number of decimal integers (up to the limitations of one's shell), including none. The program can be invoked with ./a.out 1 2 [...], as an example. main(c,v,x)char**v;{x=0;while(c-1)x+=atoi(v[--c]);printf("%d",x);} Or, more legibly: main(c,v,x) char**v; { x=0; while(c-1) x+=atoi(v[--c]); printf("%d",x); } • You don't need to have a full program. Just int f(int a,int b){return a+b;} would probably suffice. – anna328p Nov 1 '16 at 2:11 • The challenge stated program or function. I make no claims to have the best solution. Additionally, another user has already provided a standalone function, and to produce an identical one would not contribute much. – James Murphy Nov 1 '16 at 2:27 ## QBIC, 6 bytes ::?a+b It takes two cmd line params as numbers (with two :'s), names them a and b, and adds them while printing (?). ## Euphoria, 64 bytes Code: include get.e print(1,prompt_number("",{})+prompt_number("",{})) # TI-Basic, 2 bytes Very straightforward. sum(Ans # PigeonScript, 1 byte + Explanation: + pops the last two items from the stack, adds them, and pushes the result to the stack. Since there is nothing on the stack, the user is prompted for input twice. The inputs are pushed, popped, added, pushed, and the program ends, outputting what's on the stack (the result of input1 + input2) # Whirl, 32 bytes 01100100001100011110001000111100 Try it online! ### Explanation Whirl has two "rings" of operations, one for control and I/O operations and the other for math operations. The 1 command rotates the current operation ring while the 0 command switches direction of the rotation. Two 0s in a row run the current operation and switch to the other ring. 01100 Read an integer from stdin and store it at the memory pointer 100 Load that value into the math ring data storage 00 Read an integer from stdin and store it at the memory pointer 1100 Add the the values under the memory pointer to the math ring data storage 0111100 Set the control ring data storage to 1 0100 Save the math ring data storage to the memory at the memory pointer 0111100 Print the result from the memory at the memory pointer # V, 2 bytes À Try it online! You can't see it, but after the À there is a control-a character (ASCII 0x01). This answer is nice because it shows one of the nice things about V: Numeric input is more convenient. Note how one input is an argument and the other is in the "input" field. This is an allowed input method, and it saves bytes because we want to run something n times, in this case the increment command or ctrl-a. À " Arg1 times: <C-a> " Increment the next number found on this line # SmileBASIC, 13 bytes INPUT A,B?A+B • “Input: Two decimal integers. These can be given to the code in standard input, as arguments to the program or function, or as a list.” Sure your code handles input? – manatwork Jan 24 '17 at 8:43 • Ok, I fixed it. – 12Me21 Jan 24 '17 at 16:19 ## Python 3, 22 bytes lambda a,b:a+b Trivial answer for a kind of trivial question. # Taxi, 418 bytes Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:w 1 l 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Addition Alley.Go to Addition Alley:e 5 l 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:w 1 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:e 1 l 1 r. Try it online! # Wise, 9 bytes [?~-!-~]| Try it Online! Takes 2 numbers in, A, and B. Implicit input [ .... ] While the top (B) != 0, repeat: ? Move B to the bottom ~- Add 1 to the top (A) ! Move B back to the top -~ Subtract 1 from the top (B) | Gets rid of the 0 on top by ORing the top 2 Implicit output It boils down to "Add 1 to A, B times." Less cool than the other Wise answer, but also shorter. # Sinclair ZX80 BASIC (4K ROM) The usual rules about the ZX80 and its 16 bit signed integer range apply here. Anything out of this range will not add. And because PRINT A+B is too easy, I came up with two solutions: ### Method 1 ~64 bytes: This assumes that you're adding a positive integer to the first number entered: 1 INPUT A 2 INPUT B 3 IF B=0 THEN GO TO 7 4 FOR B=B/B TO ABS(B) 5 LET A=A+1 6 NEXT B 7 PRINT A ### Method 2 ~24 bytes A much simpler solution, where you are adding two -/+ integers together, simply do this: 1 INPUT A 2 INPUT B 3 PRINT A+B Both of these listings are likely to work on all variants of 8-bit BASIC, although would not be optimised on most of them. • There are no such things as functions on a Sinclair ZX80 (as far as I know anyway) so both solutions are complete symbolic listings. – Shaun Bebbers Mar 28 '17 at 20:55 # Braingolf, 1 byte + Try it online! # Triangular, 6 bytes$.$%+< Try it online! Formats into this triangle:$
. $% + < Without directionals or no-ops, the above turns into this:$$+% Triangular uses postfix notation. •$ - read input as integer
• + - add
• % - print as integer

# ,,,, 1 byte

+

Body must be at least 30 characters; you entered 14.

# Commentator, 4 bytes

////

Outputs by char code. If this isn't allowed, a 6 byte solution is

////*/

which outputs by numerical value.

# tcl, 10

expr $a+$b

If you have namespace path tcl::mathop you can do it shorter for every arithmetic operation:

# tcl, 7

+ $a$b

test case on: http://rextester.com/live/FLFPTH24568

# Little Man Computer, 23 bytes (source)

INP
STA 6
INP
OUT

## With HLT, 27 bytes (source)

INP
STA 6
INP
OUT
HLT

Online Emulator (Flash)