Functions
Introduction / Definition
A function maps inputs to a result or performs a defined computation. Functions support decomposition, reuse, abstraction, and testing. Pure functions depend only on inputs and avoid observable side effects. Other functions may intentionally interact with state.
Syntax / Structure / Formula
returnType name(parameters) { body; return value; }
Detailed Explanation
A function contract should specify inputs, output, side effects, and errors. Small functions are generally easier to test.
Example
Example: double area(double r)\{ return Math.PI\*r\*r; \}
Output / Result
For r=2, area ≈ 12.566.
Important Points / Notes
- Use clear contracts.
- Prefer focused responsibilities.
Mini Practice Question
Give one advantage of pure functions.
CheckpointTest Yourself