One character, one instruction
Every instruction in Befunge is a single character. There are no keywords, no names and no multi-character operators. That makes the whole instruction set small enough to learn in an afternoon, though using it fluently takes longer. The commands below are those of the original Befunge-93 version, which later Funge variants build on. This is a guided tour, not a full tutorial.
One convention helps with arithmetic. When an instruction takes two values, the one pushed first is called the lower value and the one pushed most recently is on top. The result is computed as lower-value operator top-value. So 82- pushes 8 then 2, and the subtraction gives 6, not negative 6.
Movement
These commands decide where the pointer goes next.
>,<,^andvset the direction to right, left, up and down.?picks one of the four directions at random, which gives you a basic random direction.#skips the next cell in the current direction.@ends the program.- A space does nothing; the pointer simply moves on.
Because the pointer keeps going in its current direction until told otherwise, a row of spaces is a harmless gap, and a missing arrow can send the pointer off in a surprising direction. The details of how this turning works are discussed further under pointer direction.
Decisions
Two commands let the pointer choose its path based on the stack.
_pops a value. If it is zero, the pointer goes right; otherwise it goes left.|pops a value. If it is zero, the pointer goes down; otherwise it goes up.
There is also the backtick, which compares two values. It pops the top two, and pushes 1 if the lower one is greater than the top one, otherwise 0. Together with the exclamation mark, which pops a value and pushes 1 if it was zero and 0 otherwise, these give you everything you need for conditional branching.
Pushing numbers
The digits 0 through 9 each push that value onto the stack. There is no way to write 12 as a single cell, so larger numbers are built with arithmetic. For example, 25* pushes 2 and 5 and multiplies them to give 10, and 99* gives 81. Many Befunge programs spend a fair amount of space building constants this way.
Arithmetic
+adds the top two values.-subtracts: lower value minus top value.*multiplies them./divides: lower value divided by top value, as whole-number division.%gives the remainder of that division.
Division by zero is not something the original language defines in a way programs should rely on. Interpreters have historically differed on what to do, so it is best to avoid it. This is one of several arithmetic edge cases that implementers have to decide on.
Stack manipulation
:duplicates the top value.\swaps the top two values.$throws away the top value.
Since Befunge has only these three tools for rearranging the stack, programs often juggle values carefully. The stack is explored more fully in the article on the stack and the playfield.
Input and output
.pops a number and prints it as a decimal integer, followed by a space.,pops a number and prints the character with that code.&reads a whole number from the user and pushes it.~reads a single character and pushes its code.
The difference between . and , trips up many beginners. If you push 65 and use ., you see the digits 65; with ,, you see the letter A. Reading numbers from a user is a topic of its own, covered under numeric input.
String mode
The double quote toggles string mode. While it is on, each cell is not run; its character code is pushed instead. The result is that a quoted word becomes a series of stack values, with the last character on top. This is the usual way to prepare text for printing, as in string mode examples elsewhere on the site.
Reading and writing the grid
Two commands let the program touch its own playfield.
gpops a y coordinate and then an x coordinate, and pushes the character code stored at that cell.ppops a y coordinate, an x coordinate and a value, and stores the value in that cell.
Using these, a program can treat spare cells as memory, which is how grid variables are usually implemented. Because the cells hold program text too, careless use can overwrite instructions, so it is a technique to use with care.
A short example using several categories
&2*.@
Here & reads a number, 2 pushes 2, * multiplies them, . prints the result, and @ stops. If the user typed 5, the output would be 10. The program touches input, arithmetic and output in only five cells.
How to remember them
It helps to group the symbols by what they look like. The arrows suggest their own direction. Punctuation like . and , is for output, and & and ~ are for input. The arithmetic symbols behave like their usual meanings. The less obvious ones are _ and |, which look like the horizontal and vertical walls they act as: one splits the pointer left or right, the other up or down.
The Funge-98 standard adds many more commands on top of these, but a solid understanding of this core set is enough to read most classic programs.