Re: Missing in misc.e

new topic     » goto parent     » topic index » view thread      » older message » newer message

don cole wrote:
> 
> CChris wrote:
> > 
> <snip>
> > global function sign(object x)
> >    if atom(x) then 
> >       return compare(x,0.0)
> >    else
> >       for i=1 to length(x) do
>         x[i]=sign(x[i])----------error------------
> >       end for
> >       return x
> >    end if
> > end function
> > </eucode>
{{{

> > for reasons very similar to the ones exposed in 5/ above.
> > 
> > HTH
> > CChris
> 
> 
> Hello CChris,
> 
>     You can't do this, can you?
>     Call a function the middle of that function?
> 
> Don Cole

Of course you can. It's called recursion and it is very common in programming.
In fact, it's a key concept of mathematics and computer science.

http://en.wikipedia.org/wiki/Recursion_%28computer_science%29

The classic example is finding the factorial of a number:

function factorial(integer f)
    if f = 1 then
        return 1
    else
        return f * factorial(f - 1)
    end if
end function


It's not always the most efficient, but sometimes it's the best.




--
"Any programming problem can be solved by adding a level of indirection."
--anonymous
"Any performance problem can be solved by removing a level of indirection."
--M. Haertel
"Premature optimization is the root of all evil in programming."
--C.A.R. Hoare
j.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu