What Is a Function?

by Carson
17 views
function graph

Functions are like computers. They accept inputs, process them, and then return outputs. Let’s explore how they work and why they’re so useful in math and programming.

How to Designate a Function?

Before introducing ourselves to the world of functions, let’s explore how to express them first. For example, a common function designation exists with the placeholder name f and the input x. We call this f(x). However, if we need to differentiate a new function from an existing one, we use a different name, like g. We might also accept more inputs, for instance, two arguments. In this case, we express our new functions as g(x, y).

Functions in programming languages follow a similar concept. We have the name of the function, and then we have the inputs enclosed in parentheses. Sometimes, you need to add keywords to identify the object as a function before we do so. Here’s a code snippet of a function in Python.

#f(x) = x
def f(x):
    return x

In this case, the “def” keyword notifies the interpreter that a function is being processed, and the “return” keyword returns the output. In C++, the keyword is different. Let’s take a look at the code below:

//g(x, y) = (x, y)
std::pair<int, int> g(int x, int y){
    return std::pair<int, int>(x, y);
}

Instead of the “def” keyword, the type of object being returned comes at the beginning of the function declaration. Note that every variable must be assigned its type before its name.

What Do Functions Do?

A function is an association between the input and output values. To visualize this, let’s think about the simplest of the relationships — that the two values are equal. In this case, we write x = y. But we can also express it as a function: f (x) = x.

From this point on, functions only get more complex. You can add, subtract, multiply, divide, exponentiate… Basically, you could do everything. You can even embed other functions within a function, such as f(x) = 2sin(x), g(x) = x*log(x), or h(x) = xcos(x).

graph of three functions
Blue: graph of 2sin(x)
Red: graph of x*log(x)
Green: graph of x^cos(x)

And then, there are the functions that don’t return an output using a constant method. They are piecewise functions, and they contain the “if” operator, which means that they evaluate the output using a function chosen based on the input value.

Graph of a function that returns cos(x) if x is negative and log(x) if x is positive
This function is formally expressed as f(x) = {cos(x) if x<0; log(x) if x>0}

Other than numerical values, they can even process other data types. In short, for every data type, we have a set of functions that processes them. In fact, this is precisely the point of making different data types — to ensure that the functions you intend to call can safely accept the input to obtain the desired output. For example, you can split a string but not a number, and you can exponentiate a number but not any other data type.

In mathematics, a function always returns something. However, the definition is taken one step further in computer programming, as some functions don’t obtain any value. Instead, they perform operations on variables so that the output modifies these variables. This can also make the code cleaner and more consistent, as every time you want to invoke the action, you don’t need to copy the same code snippet over and over again until it becomes hard to track and maintain.

Domains and Ranges

As mentioned above, functions can process all types of data. But not all functions can process all data that the data type permits. For example, there are some numbers that some functions cannot accept as inputs. To constrain that and make sure the input is valid, we use a concept known as a domain of a function.

The domain of a function is the range of values where the output is well-defined. This means avoiding dividing by zero at all times and preventing square roots and logarithms of negative numbers when dealing with real numbers. For example, the domain of the function f(x) = x/(x+1) is all real numbers except -1. If x = -1, the denominator equals zero, and the fraction becomes undefined.

Likewise, the range of a function also concerns possible values. But instead, this has to do with the output values. For example, the range of sin(x) is all values between -1 and 1. When you observe the curve of this function, it never strays higher than 1 or lower than -1, and instead reaches its maxima and minima at precisely 1 and -1, respectively.

Conclusion

In this article, we’ve introduced what functions are, what they’re capable of doing, and what domains and ranges are in terms of a function. Remember that they’re powerful tools in math and in programming to simplify operations by integrating a set of actions into a concise name. This means saving time and making the product easier to maintain.

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.