Glossary · Programming

Function

A function is a named, reusable block of code that takes input values called parameters, performs a task and can return a result to the caller.

Updated October 6, 2026

A function is a named block of code that you can call whenever you need it. It accepts input values, called parameters or arguments, does some work and may return a value. In some languages the same idea is called a method or a procedure.

Why it matters

Functions let you write logic once and reuse it, break large problems into small understandable pieces and test each piece separately. A function that does one thing and has a clear name makes a program much easier to read and to debug.

Example

def discounted_price(price, percent):
    return round(price * (1 - percent / 100), 2)

print(discounted_price(2000, 15))   # 1700.0

price and percent are parameters; 2000 and 15 are the arguments passed in the call; return sends the result back.

Common mistakes

  • Writing functions that do several unrelated things, which makes them hard to name and test.
  • Changing global state inside a function so the same call gives different results at different times.
  • Using a mutable default argument, such as def f(items=[]) in Python, which is shared between calls.

Get notified when this term updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.