Thread: Arguments
View Single Post
  #9 (permalink)  
Old 08-23-2006, 10:28 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,275
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

Functions in programming work a lot like they do in math, except programming functions get better names that f() or g().

In math, when you say f(x) = 2x, you are defining a rule: for input x, output 2 times x. In programming you have the same idea, except it can be more complicated. Because we have various data types (float, int, etc) you need to be precise about the input and output when you define your function. Also, they can do a lot more complicated stuff.

Code:
float f(float x)
{
  float temp;
  temp = 2*x;
  return temp;
}
This programming function f does the same as the math function.

-----

If we look at g(x) = 2 sqrt(x), things get more complicated.
Code:
float g(float x)
{
  float temp;
  temp = 2*sqrt(x);
  return temp;
}

float sqrt(float x)
{
  // do something
}
Notice how x gets passed into g, which has to hand it off to sqrt. This chain can happen for multiple levels, one function calling another calling another. If, at any point, one of them says "I'm not allowed to change x", then any changes that functions it calls try to make will be cancelled out when it refuses to pass those changes back.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Reply With Quote