Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
A function is a unit of code that is often defined by its role within a greater code structure. Specifically, a function contains a unit of code that works on various inputs, many of which are variables, and produces concrete results involving changes to variable values or actual operations based on the inputs.
Within a greater code structure, a function, which also may be called a subroutine or procedure, is "called" by the code, depending on a user event or as part of a greater operation. When called, the function operates on the inputs and produces the results.
A basic example is a function called "addone" that will take a variable x, which is defined as an integer, and add one. The code within addone would look something like this:
x = x + 1
An example of a function that operates on inputs instead may look like this:
if x > 5 then
Here, the function may or may not print, depending on the value of "x."