Send
Close Add comments:
(status displays here)
Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Functions
1. Functions
This page presents a discussion of functions.
2. Mathematical functions
A mathematical function is a formula that takes one or more parameters and returns one result. In mathematical functions there is no concept of a state change.
f(x) = 2*x + 1
The function
f returns or evaluates to
5 when the supplied
x is
2.
3. Arguments and parameters
Mathematicians generally call the values supplied and used, such as x above, arguments.
Computer language people generally call them parameters.
4. Functions
A programming language
function computes (or looks up) an output for a given set of inputs.
Given the mathematical
function definition
f(x) = 2 * x + 1,
The above is read as "
f of x is two times x plus one".
The
2 and
1 are literals. The asterisk "
*" is used as the multiplication operator.
5. Formal parameters
f(x) = 2 * x + 1,
In the above mathematical function definition,
f is the
function name and
x is a mathematical variable called a
formal parameter that acts as a placeholder to be supplied later.
6. Actual parameters
f(x) = 2 * x + 1
If an actual value for
x is provided, called an
actual parameter, the value of
f(x) can be determined.
The way to do this is a two step process.
Step 1: substitute actual values for formal parameters
Step 2: simplify the resulting expression
So, if
x is
3, then
f(3) is
2*3+1 is
7.
7. Example function results
f(x) = 2 * x + 1
Here are some values of
x and
f(x) determined by what is called reduction.
x f(x)
0 1
1 3
2 5
3 7
4 9
5 7
...
8. Abstraction
In general, one has a pattern and needs to abstract to a function that represents that pattern. Here is a pattern.
x f(x)
0 1
1 3
2 5
3 7
4 9
5 7
...
Now, abstract and determine a function that represents this pattern.
f(x) = 2 * x + 1,
9. Programming language functions
A function in a computer language is a piece of code that takes one or mare parameters and may return a result and may have side effects that change the memory of the computer.
From now on, functions will refer to computer language functions unless otherwise specified.
10. Other names
In general a procedure is a function that does not return a value.
A function is a procedure that returns a value.
Sometimes functions and/or procedures are called routines or methods (as port of object-oriented classes).
A very old name for functions and procedures from the FORTRAN days (1960s) is a subprogram.
11. Abstraction mechanism
A function is an abstraction mechanism that allows similar code to be shared from one place or almost similar code to be shared from one place through the use of parameters.
A function provides no additional functionality that cannot be done using variables (including arrays) and control structures (sequence, while, conditional).
Functions can be useful abstractions even if they are only called once and never in a repeated sense.
12. Expressions and statements
An expression evaluates to a value. The value need not be computed, but could just be looked up.
A statement executes for an effect (i.e., something in memory is almost always changed).
13. Returned values
Every function returns a value. If the value is empty or null (in C or C++ that empty value is void).
14. End of page
15. Multiple choice questions for this page
7 questions omitted (login required)