Mathematically, a function is a mapping from a domain set into a range set, such that any point in the domain set is mapped into a single point in the range set. Thus, for example, we can define a function f mapping the set of people into the set of colors, such that f(x) is the favorite color of the person x. Some of the points of this function might be:
x |
f(x) |
---|---|
Brian |
Blue |
Lisette |
Blue |
Grandmom |
Red |
Hikage |
Black |
Cindy |
Purple |
Mad Pumpkin |
Blue |
It is not necessary that the values of the function f(x) be distinct for distinct values of x: note how many people like blue. If all function values are distinct, though, the function is called a bijection, or a one-to-one function. For example, the positive square root function maps any non-negative number to another non-negative number, and does so uniquely: every non-negative number is the square root of exactly one other non-negative number.
In computers, a function is a subprogram that returns a value that can be used in an expression. For example, the square root operation is a function. The square root of a value x is expressed in JavaScript as Math.sqrt(x), in VBA as Sqr(x), and in the calculator as √ x.
User written functions will be covered in greater detail in the Subprograms section. However, the calculator, JavaScript, and Visual Basic for Applications all provide a set of “built-in” functions. Some of the more popular functions are given in the following table.
Name |
Mathematics |
JavaScript |
VBA |
Calculator |
---|---|---|---|---|
Absolute value |
|x| |
Math.abs (x) |
Abs (x) |
Abs x |
Square root |
√ x |
Math.sqrt (x) |
Sqr (x) |
√ x |
Natural logarithm |
ln x |
Math.log (x) |
Log (x) |
Ln x |
Common logarithm |
log x |
(none) |
(none) |
Log x |
Sine, cosine, tangent |
sin x, cos x, tan x |
Math.sin (x), Math.cos (x), Math.tan (x) |
Sin (x), Cos (x), Tan (x) |
sin x, cos x, tan x |
Arc sine, arc cosine, arc tangent |
sin^{-1} x, cos^{-1} x, tan^{-1} x |
Math.asin (x), Math.acos (x), Math.atan (x) |
(none), (none), Atn (x) |
sin^{-1} x, cos^{–1} x, tan^{–1} x |
[ Previous page | Top of page | Next page ]
Copyright © 2002 Brian Hetrick
Page last updated 3 March 2002.
Tutorial
Building Blocks I
Expressions
Functions
Control Flow II
Basic I/O
Algorithms
A First Program
Answers
Modularization
Data Structures I
Recursion
Program Attributes
Building Blocks II
Algorithm Analysis
Structuring
Data Structures II
Abstract Types
Objects
Problem Analysis
Reference Card