A function is a reusable block of code that performs a specific task. You give it a name, optionally pass it inputs (parameters), and it returns a result. Functions are the building blocks of every program — AI-generated code is organized into functions, and understanding them helps you navigate any codebase.
Functions are like recipes — they take ingredients (inputs), follow steps (logic), and produce a result (output). Nearly every line of AI-generated code lives inside a function.
function greet(name: string): string {
return `Hello, ${name}!`
}
greet("Jane") // Returns "Hello, Jane!"
greet("Alex") // Returns "Hello, Alex!"
greet (describes what it does)name (the input){ } (what it does)"Hello, Jane!")calculateTotal() is clearer than 20 lines of math| Pattern | Example | Purpose |
|---|---|---|
| Event handler | onClick() | Responds to user actions |
| Data fetcher | fetchUsers() | Gets data from an API |
| Transformer | formatDate() | Converts data to a different format |
| Validator | isValidEmail() | Checks if input meets rules |
When AI generates a function:
This four-step scan is enough to understand most AI-generated functions without deep programming knowledge.