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:
5JavaScript
function add(a, b) {
return a + b;
}
console.log(add(2, 3));Output:
5