int power (int x, int p) {
int r = 1;
while (p--) r *= x;
return r;
}
In colorForth we would do this (without color):
power 1 swap for over * next nip ;
This example does not tell the whole story however. Where in Forth we can extend the semantics of the language. C is stuck with the same syntax and semantics, which become increasingly clumsy as programs become complex. My example in C is very generous in this reguard. Look some object oriented C to see my point more fully.
Anyway, I'm not trying to bash C, only providing a counter example to the argument that programming in imperative language is necessarily ugly, or unelegant when compared with functional languages.
I do program in functional languages, but only ocationally, when I intend to run the program once or infrequently.
When writing system code or application code, it is really worth the effort to take the time and craft the code.
--- Update
Forgot the case when the power is zero. Modified the code. Note the colorForth code would need a more semantically complex "for next", since "for next" only handles a positive number of iterations.
