go to previous page   go to home page   go to next page

Answer:

Yes. The function can return a true false value (in C, non-zero or zero.)


Pure Functions

Ideally, a C function is a module that uses only its parameters to produce a value. The entry conditions describe what must be true of the parameters, and the exit conditions describe what is true of the return value. The function can be used without concern about its inner logic. Such a function is called a pure function. A pure function makes no change to any data outside of its scope. It does not change global variables or global data structures. Ideally, it does not even look at them.

An example of a pure function is the double sin(double angle) function from the standard math library. Its precondition is that its parameter is an angle in radians. Its exit condition is that it evaluates to the sine of that angle, but no data has been changed.

However, it is common in C to write functions that are not pure. In this case the entry and exit conditions must include the expected state of any global data, and the effect the function has on them.

Even though non-pure functions are sometimes necessary, for solid, reliable code you should try to make your functions as pure as possible. Sometimes main() does non-pure things, inputting data and setting things up, then main() calls a sequence of pure functions to do the work of the program.

Some programming languages, for example, "pure lisp" allow only pure functions.


QUESTION 16:

What other structures are needed in programming?