Programming Search⌘K

Conceptcore

What a program is

Also called program, computer program, source code, executable, entry point, main function, script, process, exit status, program state.

A program is text in a programming language that describes a computation. A run of it reads input, changes state step by step, writes output and ends with an exit status.

Applies to
core-languages

Idea

A program is a description of a computation, written as text in a programming language, that a machine can carry out. It names the data that the computation starts from, the steps that change that data, and the results that it gives to the world. When a program runs, it reads input, changes its state one step at a time, writes output, and ends with an exit status that tells the operating system whether it succeeded. The same text can run many times on different input, and each run is a separate process. An algorithm is the method, a program is one exact statement of a method in one language, and a process is one run of a program.

import ast

source = """
a, b = 1071, 462
while b != 0:
    a, b = b, a % b
print("gcd", a)
"""

tree = ast.parse(source)  # the text has a structure
print([type(node).__name__ for node in tree.body])

code = compile(tree, "<gcd>", "exec")  # the structure becomes instructions
state = {}
exec(code, state)  # running the instructions changes the state
print("final state:", {k: v for k, v in state.items() if k in "ab"})

edited = source.replace("1071, 462", "48, 18")  # the program is also data
exec(edited, {})
Output
['Assign', 'While', 'Expr']
gcd 21
final state: {'a': 21, 'b': 0}
gcd 6

The string source holds a program of four lines that computes the greatest common divisor of 1071 and 462 with Euclid's algorithm. Python parses the text into a tree of three statements, an assignment, a loop and a call, then compiles the tree into instructions, and running the instructions changes the state until b is 0 and a holds the answer, 21. Because the program is a string, the last lines edit it like any other data and run the new program, which gives 6 for 48 and 18. Text, structure, instructions and state are four views of the same program.

How it works

A program directs a process. Structure and Interpretation of Computer Programs opens with the distinction between the text and its run: "The evolution of a process is directed by a pattern of rules called a program. People create programs to direct processes."1 The program is fixed text; the process is what happens when a machine follows it, and it can differ on each run because the input differs. Most of the work of programming is to write text whose processes do the right thing for every input, including inputs that the author did not try.

From text to instructions. A processor cannot run text. A compiler translates the text into machine code ahead of time, an interpreter reads it and carries out each part as it goes, and many systems do both, compiling to bytecode for a virtual machine that interprets it or compiles it again at run time. See compilers and interpreters and bytecode. Each language defines what text is a valid program and what it means. The C++ reference states: "A C++ program is a sequence of text files (typically header and source files) that contain declarations. They undergo translation to become an executable program, which is executed when the C++ implementation calls its main function."2

An entry point and a boundary. Every language says where a program starts and what it contains. The Go specification says: "A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively."3 It goes on: "Program execution begins by initializing the program and then invoking the function main in package main. When that function invocation returns, the program exits."3 Scripting languages start at the top of a file instead. The Python reference says: "A Python program is constructed from code blocks."4 At its boundary a program meets the operating system: its arguments, its environment variables, its standard input and output streams, and the exit status it returns. See hello, world, command-line arguments and standard input.

State and steps. Inside the boundary, a program in the common imperative style is a sequence of steps that change state, the values of its variables and data structures. The meaning of each step depends on the state that the steps before it left. Euclid's algorithm replaces the pair (a, b) by (b, a mod b), and the example files show that the order of the updates matters: updating a before computing the remainder loses the old value and gives a wrong answer with no error. Declarative and functional programs describe the result in other terms, but they also run as steps on a machine. See imperative programming and declarative programming.

Programs are data. A program is text, so other programs can read it, check it, transform it and generate it. Compilers, editors, linters, formatters, test tools and code generators all treat programs as data, and the stored-program computer keeps the translated program in the same memory as its data. Ada Lovelace saw in 1843 that a machine that follows a program is not limited to arithmetic: "the Analytical Engine weaves algebraical patterns just as the Jacquard-loom weaves flowers and leaves."5 She also stated its limit: "It can do whatever we know how to order it to perform."5

Programs are claims. A program states, in effect, that its processes compute some result. Whether it does is a separate question, and running it on some inputs only samples the answer. Edsger Dijkstra put it in his 1972 Turing Award lecture: "Program testing can be a very effective way to show the presence of bugs, but is hopelessly inadequate for showing their absence."6 Specifications, types, tests, reviews and proofs are the ways to make the claim stronger. See unit testing and property-based tests.

In languages

LanguageVerdictNote
PythonNative
C++Native
JavaNative
CNative
C#Native
JavaScriptNative
TypeScriptNativeAs JavaScript; the compiler checks the types and emits a JavaScript program that the engine runs.
GoNative
RustNative
KotlinNativeA Kotlin program starts at a top-level main function, which the compiler places in a class for the JVM; no example file because kotlinc is not installed on the machine that recorded the examples.
SQLIdiomA script of statements and stored procedures are programs, but a single query states a result and not the steps to compute it.
BashNativeA script is a program that the shell runs line by line, and exit sets the exit status.
PowerShellNativeA script file is a program that runs from top to bottom, and exit sets the exit status; no example file because pwsh is not installed on the machine that recorded the examples.
HTMLDoes not applyMarkup describes a document, not a computation.
CSSDoes not applyStyle rules describe presentation, not a computation.
Regular expressionsDoes not applyA pattern runs inside another program and is not a program by itself.
Language Verdict Mechanism
Python native a script runs top to bottom; if __name__ == "__main__"; sys.exit
JavaScript native a script or module; process.exitCode in Node.js
TypeScript native as JavaScript, after type checking
Java native classes; public static void main(String[] args)
Kotlin native a top-level fun main
C# native one entry point; top-level statements or static Main
Go native package main and func main
Rust native a crate with fn main; ExitCode or Result
C native translation units linked into an executable; int main
C++ native as C, with constructors of globals before main
Bash native a script of commands; exit
PowerShell native a script file; exit
SQL idiom scripts and stored procedures; a query states a result
Regex n/a a pattern runs inside another program
HTML n/a describes a document, not a computation
CSS n/a describes presentation, not a computation

Every example file is a complete program that computes the greatest common divisor of 1071 and 462 and prints the state after each step: (462, 147), (147, 21) and (21, 0), then the answer 21. The algorithm is the same in each file; the programs differ in where they start, how they update two variables at once, and how they report the exit status. Python, JavaScript, C#, Go and Rust can assign both variables in one statement. Java and C keep the remainder in a temporary first, and C++ uses std::exchange.

Languages fall into two groups at the boundary. In Python, JavaScript, Bash and PowerShell, and in C# with top-level statements, a file of statements is a program, and the runtime runs it from the top. In Java, Go, Rust, C, C++ and Kotlin, a program is a set of declarations, and execution starts at a function called main that the language or the linker requires. The Go file shows that a package main without a main function is not a program at all, and the compiler rejects it.

PythonGitHub
import sys


def gcd(a, b):
    while b != 0:  # the state is the pair (a, b)
        a, b = b, a % b
        print(a, b)
    return a


def main():
    print(1071, 462)
    print("gcd", gcd(1071, 462))
    return 0  # the exit status


if __name__ == "__main__":
    sys.exit(main())
Output
1071 462
462 147
147 21
21 0
gcd 21
  • Verdict: native. A Python program is a script: the interpreter runs the statements of the file from top to bottom, so a file with one print call is a complete program. The test if __name__ == "__main__" runs main only when the file runs as a program and not when another module imports it, and sys.exit passes its value to the operating system as the exit status.
#include <iostream>
#include <numeric>
#include <utility>

long gcd(long a, long b) {
    while (b != 0) {
        a = std::exchange(b, a % b);
        std::cout << a << ' ' << b << '\n';
    }
    return a;
}

int main() {
    std::cout << "1071 462\n";
    long g = gcd(1071, 462); // the call prints its steps first
    std::cout << "gcd " << g << '\n';
    std::cout << "std::gcd " << std::gcd(1071L, 462L) << '\n';
}
Output
1071 462
462 147
147 21
21 0
gcd 21
std::gcd 21
  • Verdict: native. A C++ program is a set of translation units linked together, and it starts at main, after the constructors of global objects run. std::exchange(b, a % b) stores the new value in b and returns the old one, which updates both parts of the state in one expression. The standard library also has std::gcd in <numeric>, which gives the same answer.
JavaGitHub
public class Main {
    static long gcd(long a, long b) {
        while (b != 0) {
            long r = a % b;
            a = b;
            b = r;
            System.out.println(a + " " + b);
        }
        return a;
    }

    public static void main(String[] args) {
        System.out.println(1071 + " " + 462);
        System.out.println("gcd " + gcd(1071, 462));
        System.exit(0);
    }
}
Output
1071 462
462 147
147 21
21 0
gcd 21
  • Verdict: native. A Java program is a set of classes, and the Java launcher starts it by calling the public static void main(String[] args) method of the class it names. The compiler turns each class into bytecode for the Java Virtual Machine. Java has no multiple assignment, so the loop keeps the remainder in a local variable before it changes a. System.exit sets the exit status.
#include <stdio.h>
#include <stdlib.h>

static long gcd(long a, long b) {
    while (b != 0) {
        long r = a % b;
        a = b;
        b = r;
        printf("%ld %ld\n", a, b);
    }
    return a;
}

int main(void) {
    printf("1071 462\n");
    printf("gcd %ld\n", gcd(1071, 462));
    return EXIT_SUCCESS;
}
Output
1071 462
462 147
147 21
21 0
gcd 21

This code runs but gives a wrong answer on purpose.

#include <stdio.h>

static long gcd(long a, long b) {
    while (b != 0) {
        a = b;     /* the old value of a is lost here */
        b = a % b; /* so b becomes 0 at once */
        printf("%ld %ld\n", a, b);
    }
    return a;
}

int main(void) {
    printf("1071 462\n");
    printf("gcd %ld\n", gcd(1071, 462));
    return 0;
}
Output
1071 462
462 0
gcd 462
  • Verdict: native. A C program is a set of translation units that the linker joins into one executable, and a hosted program starts at main. The value that main returns becomes the exit status, and EXIT_SUCCESS from <stdlib.h> names the value for success. C has no multiple assignment, so the loop keeps the remainder in a temporary.
  • The wrong block overwrites a before it computes the remainder, as the JavaScript file does, and gets 462.
static long Gcd(long a, long b)
{
    while (b != 0)
    {
        (a, b) = (b, a % b);
        Console.WriteLine($"{a} {b}");
    }
    return a;
}

Console.WriteLine("1071 462");
Console.WriteLine($"gcd {Gcd(1071, 462)}");
return 0;
Output
1071 462
462 147
147 21
21 0
gcd 21
  • Verdict: native. A C# program is an assembly with one entry point. With top-level statements, the statements of one file form the body of the Main method that the compiler writes, so the file can start with code, as here. A tuple assignment updates both variables at once, and the value of return in the top-level code is the exit status.
JavaScriptGitHub
function gcd(a, b) {
  while (b !== 0) {
    [a, b] = [b, a % b]; // update both parts of the state at once
    console.log(a, b);
  }
  return a;
}

console.log(1071, 462);
console.log("gcd", gcd(1071, 462));
process.exitCode = 0;
Output
1071 462
462 147
147 21
21 0
gcd 21

This code runs but gives a wrong answer on purpose.

function gcd(a, b) {
  while (b !== 0) {
    a = b; // a now holds the new value
    b = a % b; // so this computes b % b, which is 0
    console.log(a, b);
  }
  return a;
}

console.log(1071, 462);
console.log("gcd", gcd(1071, 462));
Output
1071 462
462 0
gcd 462
  • Verdict: native. A JavaScript program is a script or a module that the engine runs from top to bottom, in Node.js from a file and in a browser from a <script> element. Destructuring assignment updates both parts of the state at once. process.exitCode sets the exit status in Node.js.
  • The wrong block updates the state one variable at a time. After a = b, the old value of a is gone, so a % b is b % b, which is 0, and the loop stops after one step with the wrong answer.
package main

import "fmt"

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
		fmt.Println(a, b)
	}
	return a
}

func main() {
	fmt.Println(1071, 462)
	fmt.Println("gcd", gcd(1071, 462))
}
Output
1071 462
462 147
147 21
21 0
gcd 21

This code does not compile, on purpose.

package main

import "fmt"

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func Main() { // Go looks for main, not Main
	fmt.Println("gcd", gcd(1071, 462))
}
  • Verdict: native. A Go program is the package main linked with every package that it imports, and it starts by calling the function main of that package. When main returns, the program exits with status 0. Go assigns both values of a, b = b, a%b at once.
  • The fails block declares Main with a capital letter. Go names are case-sensitive, so the package has no function main, and the compiler rejects it, because a package main without one is not a program.
RustGitHub
fn gcd(mut a: u64, mut b: u64) -> u64 {
    while b != 0 {
        (a, b) = (b, a % b);
        println!("{a} {b}");
    }
    a
}

fn main() -> std::process::ExitCode {
    println!("1071 462");
    println!("gcd {}", gcd(1071, 462));
    std::process::ExitCode::SUCCESS
}
Output
1071 462
462 147
147 21
21 0
gcd 21
  • Verdict: native. A Rust program is a crate whose root has a main function, and the compiler links the crate and its dependencies into one executable. main can return ExitCode, or a Result whose error gives a failing status. A tuple assignment updates both variables at once.
  • Scripts and compiled programs. A script is run from its source by an interpreter; a compiled program is translated to an executable ahead of time. Many languages do both. See compilers and interpreters.
  • Libraries. A library is code without an entry point, which programs link to or import. A program is a library plus a starting point.
  • Processes and threads. A process is a running program with its own memory; a thread is one sequence of steps inside a process. See threads and run a subprocess.
  • Interactive programs. A REPL reads, evaluates and prints one piece of a program at a time, and a server or a graphical program runs a loop that waits for events instead of ending after its output.
  • Specifications. A specification states what a program must do without saying how. Formal methods prove that a program meets one.
  • Programs that write programs. Compilers, macros and code generators take programs as input or produce them as output. See constant evaluation.
  • The theory of computation. Turing machines and the lambda calculus define which computations any program can perform, and the halting problem shows that no program can decide, for every program and input, whether it stops.

Pitfalls

  • Testing only the inputs you thought of. A program must work for every input that it accepts, including empty input, very large values and malformed text. Fuzzing and property-based tests try inputs that the author did not choose. See fuzz testing.
  • Order of updates. Updating one part of the state before another reads its old value gives wrong results with no error. The wrong blocks of the JavaScript and C files show this. Use multiple assignment or a temporary.
  • Ignoring the exit status. A program that prints an error and then exits with status 0 tells scripts and build tools that it succeeded. Return a nonzero status on failure.
  • Missing or misnamed entry points. A misspelled main, a main in the wrong package, or a Python file whose main is never called gives a program that does nothing or does not build. The fails block of the Go file shows one case.
  • Code that runs on import. Statements at the top level of a Python or JavaScript module run each time another program imports it. Keep work in functions and guard the entry point.
  • Relying on undefined behavior. In C and C++, a program that breaks a rule of the language, such as reading past the end of an array, has no defined meaning, and it can seem to work until the compiler or the input changes. See undefined behavior.

Cost

A program costs time and memory when it runs, and those costs depend on its input; the analysis of algorithms states them as functions of the input size. It also costs time before it runs. A compiled program pays for translation once, and its start is usually fast; an interpreted program pays to parse and start the interpreter on each run, often tens of milliseconds for Python or Node.js and more for a JVM before it has compiled the hot code. The size of the executable or of the installed runtime is a further cost, from tens of kilobytes for a small C program to tens or hundreds of megabytes for a program that ships with its runtime. Most of the cost of a program over its life is the work of people who read and change it.

History

Joseph Marie Jacquard's loom of 1804 followed a chain of punched cards, an early sequence of instructions for a machine. In 1843 Ada Lovelace translated Luigi Menabrea's paper on Charles Babbage's Analytical Engine and added notes, and Note G gave a table of operations for the engine to compute Bernoulli numbers, often called the first published computer program.5 Alan Turing's paper of 1936 defined computation by a machine that follows a finite table of rules, and the stored-program computers of 1948 and 1949 held their programs in memory. The word program for a plan of operations came into use with the ENIAC project in the mid-1940s.

Programs were first written in machine code and then in assembly language, and FORTRAN, from IBM in 1957, made it practical to write them in a notation close to mathematics. In the 1960s and 1970s, structured programming, which Dijkstra argued for, made the structure of the text follow the structure of the process.6 Niklaus Wirth's 1976 book had the title Algorithms + Data Structures = Programs, and Harold Abelson and Gerald Jay Sussman's Structure and Interpretation of Computer Programs, first published in 1985, taught programs as a way to express ideas about processes.1

References

  1. Harold Abelson and Gerald Jay Sussman, Structure and Interpretation of Computer Programs, second edition, MIT Press, 1996, Chapter 1, Building Abstractions with Procedures
  2. Basic concepts, cppreference.com, Introduction
  3. The Go Programming Language Specification, Program execution
  4. Execution model, The Python Language Reference, 4.1 Structure of a program
  5. L. F. Menabrea, Sketch of the Analytical Engine Invented by Charles Babbage, with notes by the translator, Ada Augusta, Countess of Lovelace, 1843, Note A and Note G
  6. Edsger W. Dijkstra, The Humble Programmer, ACM Turing Award Lecture, 1972, EWD340, Main text

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/RKMS64/text.yaml