Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Function

Функция

A function is a block of code you can call to perform a task. It can take input, return a result, or do something like write to a file.

What is a function?

A function lets you separate what a piece of code does from how it does it. For example, a checkout page can call a function to calculate shipping without knowing how the shipping rates are stored. As long as the function accepts the same inputs and behaves as callers expect, you can change how it looks up those rates without rewriting the checkout page.

What happens when you call one?

Defining a function describes what should happen; it does not run the function body. For an ordinary synchronous call, the function receives its inputs, executes its body, and returns control to the caller when it finishes normally. Parameters are the names in the definition that receive inputs. Arguments are the actual values supplied for a particular call. A function can also take no arguments.

Each call has its own local variables. That does not mean every object it works with is private: a parameter can refer to the same object the caller uses. Returning a value lets the caller store it, pass it to another function, or use it directly in an expression. A function can also finish without explicitly returning a value; the result in that case depends on the language.

Example in JavaScript

function shippingFee(subtotal) {
  if (subtotal >= 50) return 0;
  return 5;
}

const total = 42 + shippingFee(42); // 47

subtotal is a parameter, and 42 is the argument. The call returns 5, which becomes part of the addition. For a subtotal of 50, the first return exits the function immediately with 0. If you replaced that line with if (subtotal >= 50) console.log(0);, the function would print 0 but then continue to return 5.

Where functions help

You don't have to call a function from several places to justify writing it. Pulling a calculation out of a long request handler can make the calculation easier to read and test, even if it has just one caller. A useful test for shippingFee would check the threshold itself, not just a value well below it.

Some functions compute a result; others have side effects such as sending a message or updating an object. A single function can do both. Another distinction is passing a function versus calling it: shippingFee refers to the function value, while shippingFee(42) evaluates the call and produces a number. Event handlers rely on the first form so they can be called later.

How it comes up in interviews

You may be asked to trace a function with an early return, predict what happens when it changes an object, or extract a helper from a longer routine. When tracing code, keep track of three things separately: the returned value, the output printed to the console, and any state that changed.