Re: "Forward" function declarations?

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

On 8 Feb 2001, at 10:13, Michael Nelson wrote:

> 
> ----- Original Message -----
> From: "Ted Fines" <fines at macalester.edu>
> To: <EUforum at topica.com>
> Sent: Thursday, February 08, 2001 7:45 AM
> Subject: "Forward" function declarations?
> 
> 
> > I need to do the "C" equivalent of a "forward" function declaration in
> > Euphoria.  Can this be done?
> >
> 
> Not directly. A forward reference is illegal in Euphoria:
> 
> funcion foo(object x)
>     integer z
>     z=bar(x)  --syntax error "bar" not defined
>     return z+2
> end function
> 
> function bar(object y)
>     if atom(y) return -1 else return length(y)
> end function
> 
> printf(1,"%d",foo({1,2,3}))
> 
> and there is no way to declare a routine before it is defined (as there is
> in C).  Instead Euphoria uses routine_id():
> 
> integer bar_ID --bar_ID must be defined before foo()
> 
> funcion foo(object x)
>     integer z
>     z=call_func(bar_ID,{x})
>     return z+2
> end function
> 
> function bar(object y)
>     if atom(y) return -1 else return length(y)
> end function
> 
> bar_ID=routine_id("bar") -- routine_id() must be used after bar()
> 
> printf(1,"%d",foo({1,2,3}))
> 
> 
> Now this works as foo() can "see" bar_ID and bar_ID is assigned a value
> before foo() is called.

This is another example of where the interpreter can be made smarter: If the
procedure
hasn't been declared yet, look further into the code for it!! I have seen some
programs
wherein everything has been routine_id()'d, and all procedure and function calls
are to
the routine_id . The interpreter could be doing this as a default, and making
the order
of precedence irrelevant.

Kat

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

Search



Quick Links

User menu

Not signed in.

Misc Menu