Tuesday, March 22, 2011

Functions...

So the 3rd chapter  is about functions, I describe them as small programs inside a program, you "summon it" by its name and give them the information they need in order to finish their job...

The simplest example would be the addition so instead let me tell you the subtraction the function will be named "sub" and in order for sub to subtract it needs the 2 values to subtract, so you tell sub that you need to take 3 from 9, and then sub will tell you it is 6.

Functions are what libraries are made of, a bunch of functions for specific works, like OpenGL for polygon graphics, OpenAL for sound, most languages have a Math library for heavy mathematical calculations, also another for network connections and so on.

The idea of Functions is not to copy-paste so much code, but just to call it every time you need to make that job, making the program jump to look for that functionality.

The Ex. 3 from the book is done like this:

function absolute(value) {
  var result = 1;
  if (value < 0) result = value * -1;
  else result = value;
  return result;
}

No comments:

Post a Comment