Glossary

Function

A function separates one understandable operation in a program. Parameters, return values, and a clear name make it possible to reuse and test the same logic independently.

Short definition

Input, work, and result form a small contract.

A function groups steps that belong together: it calculates an item price, converts a validated payload into a data object, decides on a state transition, or builds a response. Its name should describe its purpose, while its parameters and return type should state what data it expects and what it returns.

A function does not always have to be a pure calculation. It can store data, send a message, or call an external API, thereby producing a side effect. This responsibility should be apparent from its name, return type, exceptions, and surrounding design. Calculating a price and sending an email therefore should not usually be hidden in the same small function.

What it is used for

Encapsulate one repeatable operation.

A well-defined function reduces code duplication and creates a small place for a test, review, and a later rule change.

  • calculating an order line price, discount, or tax
  • validating and converting input from an HTTP request into domain data
  • loading a specific record by a safely passed identifier
  • creating an API response or a message for a queue
  • a small infrastructure operation, such as sending a request through an HTTP client

Practical example

Calculating the price of one order item

The function accepts a quantity and unit price in cents. Types describe the technical contract, while the condition protects the domain rule that an order item must have a positive quantity.

The caller receives only the resulting amount. It does not need to know how the calculation works, and a test can verify both the ordinary case and an invalid quantity without a database or HTTP request.

PHP

function calculateLineTotal(int $quantity, int $unitPriceInCents): int
{
    if ($quantity < 1) {
        throw new InvalidArgumentException('Množství musí být alespoň 1.');
    }

    return $quantity * $unitPriceInCents;
}

How a function works

From arguments to a result or controlled error

Calling a function is a small contract. Readability depends on whether the input expectations and the function’s effect are clear.

  1. Call Another part of the program calls the function and passes arguments, such as a quantity and unit price.
  2. Parameters The function names the received values. Type declarations define which kinds of values it accepts.
  3. Local work The function creates local variables and performs one clearly defined task.
  4. Result or exception The function returns a value, completes a side effect, or signals a state the caller must handle.
  5. Using the contract The caller uses the return value and should not depend on the internal implementation unless the contract promises it.

Important concepts

Parameter, argument, and method are not the same terms.

Precise terminology makes code and discussions about interface design easier to understand.

Parameter and argument

A parameter is a variable in a function definition. An argument is a specific value passed in a call.

Return value

return passes a result back to the caller. A function can also have a void return type when it returns no meaningful result.

Function and method

A method is a function belonging to a class or object. It can work with object state and therefore has a different context from a standalone function.

Side effect

A database write, sending an email, a log entry, or an HTTP request changes the world beyond the return value. This is not an error, but the effect must be clear.

Exception

An exception signals an unexpected or impossible state. It should not replace ordinary branching or hide every validation.

Benefits and limitations

Dividing code into functions should clarify the program flow, not hide it.

Benefits

  • one place for a repeated rule and its test
  • clear input and output for calling code
  • smaller functions are easier to review and change
  • separation of a pure calculation from infrastructure or another side effect

Common mistakes

  • a function with an unclear name such as process or handle
  • too many parameters signalling a missing data object or responsibility
  • hidden reads of global state or database writes without an explicit name
  • unnecessarily splitting a simple expression into a chain of one-use wrappers

Practical boundary

A function should have an understandable reason to change.

A function is useful when it repeats a rule, gives an important step a name, or separates a part that can be tested independently. A short function is not a goal in itself: a few lines directly at the point of use may be more readable than an abstraction that adds no meaning.

For a more complex domain operation, a function is often part of an application service or object with explicit dependencies. Input supplied through dependency injection is clearer than a standalone function that secretly creates database or HTTP clients.

What to consider

The name, contract, and side effect must be clear.

A function’s quality is not judged by its line count, but by whether the caller understands its purpose, input, result, and impact.

  • use a verb-based name that describes the action or result
  • type parameters and return values where this makes the contract more precise
  • validate business rules that the type alone cannot express
  • separate calculations from the network, database, and other side effects when that simplifies testing
  • test the result and rule, not every internal implementation step unnecessarily

Common questions

Functions in practice

Are a method and a function the same thing?

A method is a function defined on a class or object. The basic principle is the same, but a method can work with the state and contract of that object.

Must every function return something?

No. Some functions perform an intentional effect and have a void return type. Even then, its name and surrounding code should make clear what it does.

Do parameter types replace validation?

No. The int type verifies the technical kind of value, but does not say that a quantity must be positive or that a user may modify a particular order.

Is it always better to divide code into the smallest possible functions?

No. The division should add meaning, reuse, or a testable boundary. Too many one-use functions can obscure a simple program flow.

How I work with PHP in practice

I divide application logic by genuine responsibility.

In backend applications, I combine small, testable functions with clearly defined services, data, and integration boundaries.

Request a call

I will call you on the next working day between 9:00 and 17:00.

You can also call me directly.

+420 605 181 728

Leave your phone number and send a callback request.

By sending, you agree to processing your data in order to handle your request.