# The Sway Reference Manual/Functions

Recall, the series of expressions we evaluated to find the y-value of a point on the line
given an x-value:
Now, suppose we wished to find the y-value corresponding to a different x-value or, worse yet, for a different x-value on a different line. All the work we did would have to be repeated. A function is a way to encapsulate all these operations so we can repeat them with a minimum of effort.

## Encapsulating a series of operations

First, we will define a not too useful function that calculates y give a slope of 5, a y-intercept of -3, and an x-value of 9 (exactly as above). We do this by wrapping a function around the sequence of operations above. The return value of a function is the value of the last thing evaluated.
There are a few things to note. The keyword function indicates that a function definition is occurring. The name of this particular function is y. The stuff between the curly braces is the code that will be evaluated (or executed) when the function is called. This code is not evaluated until then.
You can copy and paste this function into the Sway interpreter. If you do, you'll see something like:
Notice that the interpreter prompt changes to more> when the input is incomplete. In the case of the function definition above, that occurs when the curly close brace is entered.
Once the function is defined, we can find the value of y repeatedly:
The parentheses after the y indicate that we wish to call the y function and get its value.
The y function, as written, is not too useful in that we cannot use it to compute similar things, such as the y-value for a different value of x. But before we improve our function, let's modify it so that it displays the current environment. This may help you to understand what happens in a function call. while the body of the function is executing:
When we call the new version of y, we see its current environment, which has bindings for b, x, and m.
The variables b, x, and m are known as local variables since they are not directly visible outside the neighborhood of the function body.

## Passing arguments

A hallmark of a good function is that it lets you compute more than one thing. We can modify our function to take in the value of x in which we are interested. In this way, we can compute more than one value of y. We do this by passing in an argument, in this case, the value of x.
We give names to the values being passed in by placing variable names between the function definition parentheses. In this case, we chose x as the name. Notice that since we are passing in x, we no longer need (or want) the definition of x, so we delete it. Now we can compute y for an infinite number of x's:
What if we wish to compute a y-value for a given x for a different line? One approach would be to pass in the slope and intercept as well as x:
If we wish to calculate using a different line, we just pass in the new slope and intercept along with our value of x. This certainly works as intended, but is not the best way. One problem is that we keep on having to type in the slope and intercept even if we are computing y-values on the same line. Anytime you find yourself doing the same tedious thing over and over, be assured that someone has thought of a way to avoid that particular tedium. So assuming that is true, how do we customize our function so that we only have to enter the slope and intercept once per particular line? We will explore three different ways for doing this. In reading further, it is not important if you understand all that is going on. What is important is that you know other approaches exist and understand the pros and cons of each approach:

## Creating functions on the fly

Since creating functions is hard work (lots of typing) and Computer Scientists avoid hard work like the plague, somebody early on got the idea of writing a function that itself creates functions! Brilliant! We can do this for our line problem. We will tell our creative function to create a y function for a particular slope and intercept! While we are at it, let's change the variable names m and b to slope and intercept, respectively:
The makeLine function creates a local y function and then returns it. This next version is equivalent:
Since the last thing makeLine does is to define the y function, the y function is returned by a call to makeLine.
So our creative function simply defines a y function and then returns it. Now we can create a bunch of different lines:
Notice how lines a and b remember the slope and intercept supplied when they were created. While this is decidedly cool, the problem is many languages (C and Java included) do not allow you to define functions that create other functions. Fortunately, Sway does allow this.

## Using objects

Another approach to our line problem is to use something called an object. In Sway, an object is simply an environment and we have seen those before. So there is nothing new here except in how to use objects to achieve our goal. Here, we define a function that creates and returns a line object. A function that creates and returns an object is known as a constructor.
The this variable always points to the current environment, which in this case includes the bindings of the formal parameters slope and intercept. By returning this, we return the environment of line, and we can look up the values of slope and intercept at our leisure. To prove that slope and intercept exist, we can use the built-in pretty printing function, pp:
We access the variables in an object with the '.' (dot) operator:
Now we modify our y function to take in a line object as well as x and use the dot operator to extract the line's slope and intercept:
In this scenario, we create different lines, then pass each line to our new y function:
The problem with this approach is we have separated line objects from finding y values, yet these two concepts are closely related. As an example, suppose we have parabola objects as well as line objects. Our y function would fail miserably for parabola objects even though the concept of (x,y) points on a parabola is just as valid as points on a line.
In the object-oriented world, we solve this problem by bundling the object and functions that work specifically on that object together. In our case, we make the y function part of the line object:
This is very similar to the functions-on-the-fly approach, but we return this instead of the function bound to y. Now we call the y function via the line object.
Should we have a parabola object, it would have its own y function with a different implementation. We would call it just the same, however:
This approach is supported in object oriented languages such as Java. The earlier approach (where the function was separated from the object) is supported in procedural languages such as C.

## Functions versus operator

All operators are functions and can be called using operator syntax. For example, the following expressions both sum the values of a and b:
Conversely, any function of two arguments can be called using operator syntax. Sometimes using operator syntax makes your code more clear. Let's make a function that increments a variable by a given amount, similar to the C, C++, and Java operator of the same name:
Don't worry about how the code works; just note that the += function has two formal parameters ($v and amount) and thus takes two arguments. We can call += to increment a variable using function call syntax:
or we can use operator syntax:
In both cases, the output of the code fragments is the same:
Functions that are called using operator syntax have the same precedence level as the mathematical operators and are left associative.

## Footnotes

* ↑ Typing a long construct into the interpreter is tedious. In a later chapter, we will learn how to store our code in a file and have the interpreter execute the code in that file. If we need to make changes to our code, we simply edit the file. That way, we do not need to type in the modified code into the interpreter from scratch.
* ↑ Sway lets you redefine variables and functions.
* ↑ The local function y has, as its context, the local environment of the makeLine function. This environment holds the bindings for slope and intercept.
* ↑ Here is a concrete example of trying to generalize, so that our function works for all objects for which the concept of the function is valid.
← Assignment · Storing Code in Files →