The Basics Of Functions

Phinn

So you wanna be a demo coder huh? You've always had a nack for math and somehow you've learned to program, and you're getting better at it. Then one day you stuble accross this thing called a demo. What is it? What does it do? You download it. You watch it. You're amazed. You think "I want to do that," "I could do that." Well, my question to you now is what are you willing to sacrifice? How good do you really want to be?

One of the keys to becoming a great demo coder is understanding the math behind the scene. In fact mathematics is the driving force of everything (although a physist will tell you other wise). That's why I'm here, to help all of you reading, especially the younger people reading to understand math, and not just the math used in demos, although that will be the main focus of these tutorials. So, in other words, I plan on covering a lot of ground here, starting with the most basic priniciples and working on up to some very advanced things like liner algerbra, and numerical analysis. I'll not only cover algebra based math, but also discrete math, the stuff mose math and computer science majors and university don't see until their second year.

Like I said before not all of this stuff will directly relate to demo coding but it will make you a better coder once you understand it. What was that? Once I understand it? Yep, once you understand it, this stuff will be tough at times, both for you to understand and for me to write, and it will never get easier... cheer up saints it's gonna get worse... but when it's all said and done and we look back at this, you'll wonder why you had such a hard time understanding it, and I'll wonder why I had such a hard time writing it.

Before I begin I'm gonna have to make a few assumptions, like you know how to add, subtract, multiply and divide, and that you understand what a varible is and that you also know some basic notation, so if I say something like x^2, you know that that means any value x squared (x * x). So, enough with the preliminaries, let's get on with business.

In this first tutorial I'm going to cover the concept of a fuction. Note that I didn't say fuctions, but the *concept of a function*, because to say I'm going to cover fuctions means that I'm going to basically cover every mathematical concept, ranging from partial differential equations to linear equations. So needless to say, the concept of a fuction is probably the most important concept in math and computer science.

Simply put a function is a give and take situation, you give it some stuff, and it gives you some stuff back. Take for instance a putpixel fuction, you give it where you want the pixel to go, its colour, and the almost by magic a pixel is on your screen. Mathematically, you give a function a number, or two, or three or on and on, and it gives you a number back. It might look like this f(x)=x^2, another common way to denote f(x) is to say y instead, so f(x)=x^2 becomes y=x^2. f(x), pronounced f of x, says f is a function of x, and f is equal to x^2. Curious notation, isn't it... f(x), and all it really means is that f is a funtion of x. This leads to something a little more curious, what is a fucntion, what do I mean by the word function? ... Well, errrrr ... I can't really say, in fact most mathematicians choose not to define what function means because somewhere in that definition will be a word that needs to be defined, and it that definition will be another word... and so on. So, instead of defining what a funtion is in rock solid dictionary form mathematicians choose to just give the properities something needs to have in order to be a function.

Side Note: Functions aren't the only things that are left undefined in mathematics, so are sets, points, and lines. I'll cover these things in later tutorials.

So now the question becomes, what are the properties of a funtion? I've already given one, it returns something, for example our putpixel function returns a pixel, and f(x)=x^2 returns 4 if x is 2 (this is denoted by f(2)=4). Some of the other properties of a function are that it is either surjective, injective, or both, which is call bijective. To be surjective means that for 2 seperate inputs, the function returns the same output, such as f(a)=1 and f(b)=1, f(x)=x^2 is surjective, can you see why? Injective means for every input there is one output, so f(a)=1 and f(b)=2, another term for an injective function is one-to-one. An example of an injective funtion is f(x)=x. Bijections are a little more tricky, they are both by bijective and surjective and they always have an inverse. To find the in verse of a funtion swap the x and the y and then solve for y.

Example:

Find the inverse of y=x+3;
swap x and y: x=y+3
solve for y: x-3=y
so the inverse of y=x+3 is y=x-3;

As you all probably know, functions such as f(x)=x^2 can be graphed. To graph such a function you would need an axis, and for y=f(x)=x^2 the xy plane will do nicely ...

                                      y

                                      ³
                                      ³
                                      ³
                                      ³
                                      ³
                                      ³
                 ---------------------Å---------------------  x
                                      ³
                                      ³
                                      ³
                                      ³
                                      ³
                                      ³

TADA, the xy plane :) ...

Now to graph the function there needs to be some points to put on the axis. To find these points run values through the funtions till you have enough points to draw a smooth line through them. This can take anywhere from 3 points to a million.

Here's a chart of points for y=x^2:

    y   ³   x
 -------Å-------
    -3  ³   9
    -2  ³   4
    -1  ³   1
     0  ³   0
     1  ³   1
     2  ³   4
     3  ³   9

And the points plotted would look some thing like this:

                                        y

                                  X     ³     X
                                        ³
                                        ³
                                        ³
                                        ³
                                    X   ³   X
                                        ³
                                        ³
                                      X ³ X
                   ---------------------X---------------------  x
                                        ³
                                        ³
                                        ³
                                        ³
                                        ³
                                        ³

Can you now see why X^2 is surjective?

Needless to say it would take a very long time to graph funtions of any complexity, and that's why graphing calculators were invented. However since this is a coding tutorial perhaps the best thing to do would be to write a program that will graph functions for us. Perhaps there will be some code for this in the next installment if I find out people are reading and learning from what I write.

Now I've pretty much exhausted the basic concept of a function... at least I think I have... Hope you all enjoyed it and learned something from it. If I didn't cover something in suffient detial let me know and I'll try to help you to understand, you can email me at phinn@uswest.net. Phinn Out.

- Phinn