Skip to main content

Overview of Functions

Imagine you have a blender. To your blender, you add a couple scoops of ice cream and some milk. Then you press the buttons on the blender. Soon, you have a delicious vanilla milkshake. Next, you add strawberries and press buttons again. The result is even better than before: a strawberry milkshake.


Input
Output
Of course, strawberries aren't the only option. Suppose that, instead of strawberries, you added cocoa powder and avocado. The result is a chocolate-avocado milkshake. Or you could have added some mint leaves, for a mint milkshake. Or some peaches, for a peach milkshake.

In each of these cases, you pick the ingredient, and get a different milkshake. Whatever ingredient you pick, the result is always a milkshake. In other words, the blender took your ingredients, and returned a flavor of milkshake.

The blender is like a function.


In math, a function takes a number, and follows a set of rules to do something to that number. No matter what the number is, the function always follows the same rules.

Here's an example of a mathematical function:
f(x) = 2x + 3


Let me explain how this works. This function is being represented by the letter f. Any other letter would work, but I picked the letter f for this function. The function is going to take a number, multiply it by 2, and then add 3.

We can replace the x with any number. I'll try 4:
f(4) = 2(4) + 3 = 11


So f(4) is equal to 11.

Here's another example of a function:
g(x) = f(x + 1)


In this case, the function g is defined using another function f. We'll assume that this f is the same as the f I mentioned a little while ago. So what is g(4)? I'll work it out:
g(4) = f(4 + 1)
= f(5)
= 2(5) + 3
= 13


So g(4) = 13.



It is often helpful to visualize data from a function. One way of doing this is by listing values of f for different values of x in a table:

x f(x)
0 3
1 5
2 7
3 9
4 11
5 13
6 15

Tables can be very useful for organizing data.

Another method for visualizing data from a function is to graph it:
Parametric Plot
Min x: Max x:
Min y: Max y:

y(x) =
Other functions:
Step size:
Above, you can see a graphing application I wrote that will graph a function y(x). The whole graph is divided into a grid; at a given point on the graph, the x-value is the distance from the left side of the graph, and the value for y(x) is the distance from the bottom.

The default function being graphed is y(x) = 2x + 3.

Using a graph, we can find a value of y(x) for any value of x. To find y(3), start at the lower left corner, and move 3 units to the right. Then, move up until you get to the line. You'll have to go up 9 units to get to the line. Thus, y(3) = 9.

We can find y(1) by moving 1 unit to the right, and then moving up 5 units to the line: y(1) = 5. And we can find y(0) by starting all the way at the left, and moving up 3 units to the line.


Now how are functions useful?


Suppose you need to figure out how much to charge for a pizza (in dollars) based on the diameter (in inches). You could use the following function:
p(s) = s


So a 10" pizza would cost p(10) = $10, and a 14" pizza would cost $14. In other words, the pizza costs a dollar per inch.

But that doesn't make sense, mathematically. Nobody cares how wide the pizza is - they care how much there is. Instead of charging by the diameter, you should charge by the area. How about $0.10 per square inch? You'll need to use the formula for the area of a circle (remember that radius is half the diameter):
p(s) = π(s/2)2*0.10


In this case, a 10" pizza would cost π*25*0.10 ≈ 3.14*25*0.10 ≈ $7.85, and a 14" pizza would cost $15.39. These prices make much more sense.

You can also use the function the other way around. If you can calculate the price per square inch of a small pizza, you can determine the pizza-pricing function. Then you can use the function to calculate the optimum prices of other sizes of pizza (relative to the small pizza). Then check whether your calculated prices match the actual prices. Interestingly, large pizzas are usually cheaper (by area).

By using functions, you might actually be able to save money.

So math isn't only for programmers and physicists. It can be used by everyone else too.


Take Calculus for example...
But that'll have to wait.


New posts every month - subscribe for free!


Comments

Popular posts from this blog

Flipping Quarters

Here's an interesting puzzle involving chance: A man in a park asks you to play a game with him. It's a form of gambling. To play, you must pay the man $5, then flip a coin repeatedly until you get heads. As soon as you get heads, you stop flipping. If you only flipped the quarter once, he'll give you $1. If you flipped it twice, you get $2. Three times, $4. Four times, $8. Each extra flip gets you twice as much money, so the longer it takes before you get tails, the more money you get. Should you play, if you have a lot of time and the man will play as many games as you want? How much money, on average, would you gain (subtracting the $5 fee)? I will give the solution in a later post .

Pluto No Longer on the Horizon

This morning, New Horizons became the first spacecraft to make a flyby observation of the Pluto system. During the mission, the spacecraft captured the most detailed photographs of Pluto's surface we've ever had, and possibly ever will have. It also found many new properties including size, mass, atmosphere, and surface composition. In a period of a few hours, we discovered more about Pluto than we've found in the 85 years since Clyde Tombaugh captured its first photograph. Before After  (images credit: NASA) To complete this mission, the spacecraft flew for more than 9 years through the emptiness of space. This may sound like a long time, but it's actually amazingly quick. In fact, New Horizons set the record for the fastest speed at launch, and during the flyby, the spacecraft was moving at a rate of over 30,000 mph, or roughly 50 times the speed of sound. Picture an object twice as heavy as a grand piano moving 25 times faster than a bullet from a gun. Yikes. The man...

Should Tau Replace Pi?

The digits of π, organized in a very new way Happy π-day! And happy π-month! Today's month and day - that is, March 14 or 3.14 - includes the first 3 digits of π. And today's month and year - March 2014 or 3.14 - also includes the first 3 digits of π. We won't have another double-day for π for the next 100 years, so enjoy this one! For the special occasion, I'm posting two π-related posts - one for π-month, and the other for π-day. In both posts, I'm setting the font size to approximately π * π + π + π. This is the first post, for π-month; to see the second, go to http://greatmst.blogspot.com/2014/03/pi-month-pi-day-post-2-5-common-pi-myths.html . In this post, I am including an essay I wrote about whether π or τ is the more superior constant. This was written for people who know very little about math, so the basic idea should be easy to understand even for people who are not mathematically inclined. Should Tau Replace Pi? A constant is any number or value that ne...