In this article, we are going to discuss what a function is, the ways a function can be used, and how it relates to programming. In essence, a function refers to how the values of one variable parameter relate to the outcome and amount of another parameter.
In certain mathematical equations, functions are inputs that output one specific result. A typical example of this is used in mathematics where (x) and (y) are used as variables.
For instance, when graphing, we use a grid with both horizontal and vertical lines. The horizontal lines are the (x) variables, and the vertical lines are the (y) variables. In the example below, we can see the price reduction is SSD pricing over time:
In this case, the cost of the SSD range is from $100 to $400 and is represented by the y-axis. The x-axis in this graph represents the date/year time frame. This graph shows the price drop from January 23rd, 2018 to December 8th, 2019, of “Samsung’s SSD 860 EVO 1TB” drive. For this illustration, using November 6th, 2018 as our (x) input, we can see the cost of this SSD was approximately $175 (y) output.
Why are Functions Important?
Some of the advantages of utilizing functions are:
- Functions reduce unnecessary redundancy in formula or code.
- Functions break up complex problems into simple, more concise objects.
- Functions improve the transparency and understanding of the code.
Another common example of a function used in mathematics is
f(x) = x
The term “f(x)“ is a different way of saying “(y)“. For instance, in the following table of US gas prices for a gallon of gas or 3.8 liters, each “(x)” input has a paired “(y)” output:
|Year (x)||Cost Of Gas Per Gallon f(x)|
Using the graph above:
f(1998) = $1.03
f(2018) = $2.72
A more diverse option would be using a piecewise-defined function:
Using the above function:
If (x) = (6) the equation is (62)=(36)
if (x) = (8) the equation is (8) + (7) = (15)
Examples of Programmatic Functions
Programmatically speaking, a function is a reusable section of code that can be defined by its use inside a larger software construct. A function is primarily comprised of a part of code that works with various inputs, most of which can be variables, that produces a consistent result that involves modifications to changeable values or actual operations, based on the original inputs.
What Defines a Good Function?
A good function:
- Has a unique and relevant name.
- Includes a string literal that is used as a comment to document a specific segment of code.
- Always delivers a value.
- Is short, descriptive and is usually less than 50-60 lines.
- Has a single, distinct task.
- Is idempotent. (is an attribute of certain activities in math and computer science where a function can be used over and over again without changing the result unless the initial arguments are modified)
def functionname( parameters ): "function_docstring" function_suite return [expression]
In Python, a function links a block of code to a variable using the “def” keyword:
To execute this code in Python, we type the function’s name and append parenthesis:
And the resulting output, “Hello from Liquidweb!” is produced.
The Python programming language accepts multiple types of parameters inside functions. Another type of function are lists:
Python also accepts the recursion function, meaning a function can call upon itself. Recursion is a familiar concept used in both mathematics and programming. In the following example, we use an instance of recursion to create a factorial to count all the different possibilities when shuffling a deck of (52) playing cards.
To confirm the mathematical equation earlier in this article:
the same equation has been rewritten in Python using functions:
Def Function Example
Here is an example of a sales tax calculator which applies a function to calculate the total cost of an item, including the sales tax:
A full list of the latest built-in Python functions and types can be found here.
In conclusion, we hope the preceding examples help demonstrate how a function is defined by pairing multiple or diverse inputs to only one output, whether it occurs in mathematics or programming. In mathematics, we saw functions that are used to plot graphs. In Python, functions can accept keywords or integers and make storing routine tasks simple. This component is useful as it allows a programmer to call on and repeat tasks defined by a function during various parts of their script, versus re-writing the same code over and over.
Our newest servers run Python natively and can take advantage of all the power Python has to offer. Searching for a dependable development platform? Looking for a reliable, safe and robust production foundation for your framework? Look no further. Liquid Web provides both in a single durable efficient Dedicated or Cloud Dedicated server.