Programming

function

A function is a reusable little machine. You feed it some inputs, it does one job, and it hands you back a result. Write it once, then call it by name anywhere you need that job done — no need to spell out the steps again.

Think of a coffee machine: water and beans go in (the inputs), it runs its routine, and coffee comes out (the result). You don't rebuild the machine each morning — you just press the button.

This is how programs stay tidy and readable. Instead of copy-pasting the same ten lines all over, you wrap them in a named function and call it. Fix the logic in one place and every caller gets the fix for free.

function greet(name) {
  return "Hi, " + name
}
greet("Sam")  // "Hi, Sam"

Define the machine once; call it with any name you like.

A function that returns the same output for the same input, with no side effects, is called 'pure' — the easiest kind to trust and test.

Also called
methodproceduresubroutine