What Is A Function?

Reading Time: 3 minutes

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. 

Mathematical Functions

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:

samsung_ssd

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.

Mathematical Functions

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)
1998
$1.03
2008
$3.25
2018
$2.72

Using the graph above:

     f(1998) = $1.03
Or
     f(2018) = $2.72

A more diverse option would be using a piecewise-defined function: 

Note:
A piecewise function is a procedure built from parts of different functions over several intervals.
functions-12-11-19

Using the above function:  
    If (x) = (6) the equation is (62)=(36)
Or
     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:

  1. Has a unique and relevant name.
  2. Includes a string literal that is used as a comment to document a specific segment of code.
  3. Always delivers a value.
  4. Is short, descriptive and is usually less than 50-60 lines.
  5. Has a single, distinct task.
  6. 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)

Functions Syntax

def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]

Def Function

In Python, a function links a block of code to a variable using the “def” keyword: 

def-liquid-function

To execute this code in Python, we type the function’s name and append parenthesis:

liquid-function-hello

And the resulting output, “Hello from Liquidweb!” is produced.

List Function

The Python programming language accepts multiple types of parameters inside functions. Another type of function are lists:

def-liquid-list-function2

Output:

def-liquid-list-function3

Recursion Function

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. 

Note:
A factorial is the product of an integer and all the integers below it; e.g. the factorial of (4) is (1)*(2)*(3)*(4) = (24)
def-liquid-list-function4

Output:

def-liquid-list-function4-output

To confirm the mathematical equation earlier in this article: 

functions-12-11-19

the same equation has been rewritten in Python using functions:

def-exponent-def-add

Output:

run-liquid-keyword-py
run-liquid-keyword-py2

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:

sales-tax-calc

Output

sales-tax-calc2

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. 

Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions Team or an experienced Hosting Advisors today!

Author Bio

About the Author: Michael Gearhart

Michael is a Linux Support System Administrator at Liquidweb, where he has led, trained, and developed Enterprise level System Administrators in the past. Currently, he supports all things technical, primarily working with corporations that utilize engineered services. 

Michael has worked on various projects for prominent companies in the past such as the American Red Cross, Ubisoft, and TracFone Wireless. Michael helps technology companies implement sophisticated, multi-node configurations. These can include load balancers, firewalls, VPNs, and switches in addition to complex software for redundancy and replication to ensure 100% uptime. With over fifteen years of Linux system administration experience, Michael is often a go-to asset for technical knowledge when it comes to troubleshooting software and hardware issues. 

Michael is a biker and nature enthusiast from Lansing, Michigan who loves to explore. Please email kersmaka@gmail.com or go to https://www.linkedin.com/in/Michael-Gearhart/ to contact Michael. 

Get 33% off the first 3 months on a new Dedicated server! Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7x365 Support