Skip to content
UPEEC

Functions

A function packages reusable logic behind a name, with inputs (parameters) and an output (return value).

Beginner
Comments 0

Functions (also called methods or procedures) let you:

  • avoid repeating code,
  • give a meaningful name to a piece of logic,
  • test parts of a program separately.
def ohms_law_current(voltage, resistance):
    return voltage / resistance

print(ohms_law_current(5, 220))   # 0.0227 A

A good function does one thing, has a clear name and avoids hidden side effects.

Sub-topics (2)

Recursion

A recursive function solves a problem by calling itself on a smaller version of the same problem.

Easy♥ 0🔖 0💬 0

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.