Skip to main content

Function

In Turkish
Fonksiyon
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/function

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

Calling a function: area(3, 4) passes the arguments 3 and 4 into the parameters width and height, the body computes width × height, and return sends 12 back to the caller. Calling it again with 5 and 2 reuses the same code and gives 10.callarea(3, 4)argumentsfunction area(width, height)return width * height// 3 * 4return value12area(5, 2)10same code, new arguments
Parameters are the names in the definition; arguments are the values in a call. Write the function once and call it wherever you need it.

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 return statement sends a result back to the caller.
  • A method is a function attached to an object or class.

Example

Defining and calling a functionjavascript
// 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings