Function
- In Turkish
- Fonksiyon
In short
A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
What is a function?
A function packages a set of instructions under a name so you can run them whenever you need them, just by calling that name. It can accept inputs, called parameters, and send back an output, called a return value. Functions are one of the most basic building blocks of almost every programming language.
A kitchen blender is a good analogy for a function. You put ingredients in (the arguments), press the button (call the function), and get a smoothie out (the return value). You don't need to think about how the motor works each time; you just use it.
Functions help you avoid repeating code, break big problems into smaller pieces, and test each piece on its own. A good function usually does one clear job and has a descriptive name like calculateTotal or sendEmail. In languages like JavaScript, functions are also values: they can be stored in variables, passed to other functions, and returned from them.
A method is a function that belongs to an object or class, such as user.save() in object-oriented code. Parameters and arguments are also often mixed up: parameters are the names listed in the function definition, while arguments are the actual values passed in when the function is called.
At a glance
Key takeaways
- A function is a reusable, named block of code that does one job.
- Parameters are the inputs a function declares; arguments are the values you pass in.
- A
returnstatement sends a result back to the caller. - A method is a function attached to an object or class.
Example
// Define a function with two parameters
function calculateTotal(price, taxRate) {
const tax = price * taxRate;
return price + tax; // send the result back to the caller
}
// Call it with two arguments
const total = calculateTotal(100, 0.2);
console.log(total); // 120
// Arrow function: a shorter syntax for the same idea
const double = (n) => n * 2;Readers ask
What is the difference between a function and a method?
A method is a function that is attached to an object or class and is called through it, like list.push(4). A plain function stands on its own and is called directly, like calculateTotal(100, 0.2).
What is the difference between parameters and arguments?
Parameters are the variable names listed in a function's definition. Arguments are the actual values you supply when you call the function.
What is a pure function?
A pure function always returns the same output for the same input and has no side effects, such as changing global variables or writing to a file. Pure functions are easier to test and reason about.
See also
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- RecursionProgramming Fundamentals, p. 48Recursion is a technique in which a function solves a problem by calling itself on smaller versions of the same problem until it reaches a simple base case.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- CallbackProgramming Fundamentals, p. 7A callback is a function passed as an argument to another function, which then calls it later, for example when a task finishes or an event happens.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- AlgorithmProgramming Fundamentals, p. 2An algorithm is a finite, step-by-step set of instructions for solving a problem or completing a task, such as sorting a list or finding the shortest route.
- ParameterProgramming Fundamentals, p. 43A parameter is a named input in a function's definition, and an argument is the actual value passed in that place when the function is called.
- MethodProgramming Fundamentals, p. 37A method is a function that belongs to an object or class and is called on it, so it can read and change that object's data, as in user.rename("Ada").
Spotted a mistake or something missing on this page?Suggest an edit