Functions
A function packages reusable logic behind a name, with inputs (parameters) and an output (return value).
Beginner
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)
Parameters and Return Values
Parameters carry data into a function; the return value carries the result back out.
Beginner♥ 0🔖 0💬 0
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.