Function

A function is a reusable, named block of code that performs a specific task. It can take inputs (parameters) and return an output.

General Programming

Functions let you write logic once and call it many times, which keeps code organised and avoids repetition (the DRY principle). They are the main way programs are broken into smaller, testable pieces.

Syntax

function name(parameters) { return value }

Examples

Python

def add(a, b):
    return a + b

print(add(2, 3))

Output:

5

JavaScript

function add(a, b) {
  return a + b;
}
console.log(add(2, 3));

Output:

5

Learn it in a lesson

See also

Back to the Dictionary