What is a function?
A function is a named, reusable block of code that takes some inputs, does something with them, and usually returns a result.
Think of a function the way you would think of a recipe. The recipe has a name, it expects certain ingredients, it lists the steps to follow, and it produces a known result. Once you have written it, you can use it again and again without rewriting the steps.
In code, functions are how you avoid repeating yourself. If your app needs to format a price three different times, you write one function called something like formatPrice and call it from each place. If you ever need to change how prices look, you change the function once and every screen updates.
When you work with AI coding tools, the AI will often suggest pulling repeated code into a function. Saying yes to that is usually a good idea, because shorter, named blocks are easier for both humans and AI to keep track of later.
Example
function formatPrice(amount) {
return "$" + amount.toFixed(2);
}
formatPrice(9.5) // "$9.50"Related terms
Want to use Function in real work?
WeCode workshops are built around AI coding tools. Pick a tier, or browse more glossary entries to get the lay of the land.
