1. Newcomer with a problem
Hello, everyone. I'm new to Euphoria, and have been having a problem
that I can't seem to find the answer to in the documentation. It's
probably there, I'm just horrible at finding things like that.
I'm wondering about function and procedure declaration. Currently,
I've got a function and a procedure. They both call the other at one
point in their block, but because one appears before the other, the
interpreter says the second one is undefined (and therefore will not
continue running the program).
I know that C++ has function prototypes. Does Euphoria have anything
similar that allows for function/procedure/type declarations?
Here's a sample of the code for the function and procedure, although
I don't feel it will be relevant (and yes, I do realize it's probably
not done in the most efficient manner. I'm new, after all!):
procedure check_menu_input(integer x)
if x < 1 or x > 2 then
menu()
-- The line above is the problem causing line. The interpreter says
-- menu() has not yet been declared.
elsif x = 1 then
story1()
elsif x = 2 then
story2()
end if
end procedure
function menu()
-- Story menu
printf(1, "\n\nPlease choose one:",0)
printf(1, "\n1. A visit to the _",0)
printf(1, "\n2. Dear Teacher",0)
menu_num = waitkey(0)
check_menu_input(menu_num)
end function
Thanks in advance,
Matt Williams
2. Re: Newcomer with a problem
>Hello, everyone. I'm new to Euphoria, and have been having a problem
>that I can't seem to find the answer to in the documentation. It's
>probably there, I'm just horrible at finding things like that.
>
>I'm wondering about function and procedure declaration. Currently,
>I've got a function and a procedure. They both call the other at one
>point in their block, but because one appears before the other, the
>interpreter says the second one is undefined (and therefore will not
>continue running the program).
>
>I know that C++ has function prototypes. Does Euphoria have anything
>similar that allows for function/procedure/type declarations?
>
>Here's a sample of the code for the function and procedure, although
>I don't feel it will be relevant (and yes, I do realize it's probably
>not done in the most efficient manner. I'm new, after all!):
>
>procedure check_menu_input(integer x)
> if x < 1 or x > 2 then
> menu()
>-- The line above is the problem causing line. The interpreter says
>-- menu() has not yet been declared.
And the interpreter's right :) You see, you _HAVEN'T_ declared menu()
yet.
*SNIP*
>function menu() <---- This is where you declare menu()
*SNIP*
>end function
So, all you have to do is to declare menu() before check_menu_input()
like so:
function menu()
codecodecode
end function
procedure check_menu_input()
codecodecode
end procedure
I think you could also use routine_id() to get over the problem, but I
don't know how to use it, so you'll have to consult library.doc for more
help.
-Tom
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com
3. Re: Newcomer with a problem
Wrote:
> >Hello, everyone. I'm new to Euphoria, and have been having a problem
> >that I can't seem to find the answer to in the documentation. It's
> >probably there, I'm just horrible at finding things like that.
> >
> >I'm wondering about function and procedure declaration. Currently,
> >I've got a function and a procedure. They both call the other at one
> >point in their block, but because one appears before the other, the
> >interpreter says the second one is undefined (and therefore will not
> >continue running the program).
> >
> >I know that C++ has function prototypes. Does Euphoria have anything
> >similar that allows for function/procedure/type declarations?
> >
> >Here's a sample of the code for the function and procedure, although
> >I don't feel it will be relevant (and yes, I do realize it's probably
> >not done in the most efficient manner. I'm new, after all!):
> >
> >procedure check_menu_input(integer x)
> > if x < 1 or x > 2 then
> > menu()
> >-- The line above is the problem causing line. The interpreter says
> >-- menu() has not yet been declared.
> And the interpreter's right :) You see, you _HAVEN'T_ declared menu()
> yet.
>
> *SNIP*
>
> >function menu() <---- This is where you declare menu()
> *SNIP*
> >end function
>
> So, all you have to do is to declare menu() before check_menu_input()
> like so:
>
> function menu()
> codecodecode
> end function
>
> procedure check_menu_input()
> codecodecode
> end procedure
The only problem with that is that menu() calls check_menu_input()
AND check_menu_input() calls menu()!
> I think you could also use routine_id() to get over the problem, but I
> don't know how to use it, so you'll have to consult library.doc for more
> help.
I'll try to sort through the documentation to find routine_id(), but
if anyone else happens to know the exact solution to my problem, I
would appreciate you emailing me.
Thanks in advance,
//|//|att |//|//illiams
;
| "Sailor Moon Forever!"
|
| Fan of Ranma 1/2 O_O |
\----------------------------/
4. Re: Newcomer with a problem
At 01:50 25/01/98 +0000, David B. Williams wrote
>Hello, everyone. I'm new to Euphoria, and have been having a problem
>that I can't seem to find the answer to in the documentation. It's
>probably there, I'm just horrible at finding things like that.
>
>I'm wondering about function and procedure declaration. Currently,
>I've got a function and a procedure. They both call the other at one
>point in their block, but because one appears before the other, the
>interpreter says the second one is undefined (and therefore will not
>continue running the program).
>
Unfortunately mutual recursion is not allowed in euphoria.
A routine may call itself but not another routine that calls it.
Graeme.
5. Re: Newcomer with a problem
>Hello, everyone. I'm new to Euphoria, and have been having a problem
>that I can't seem to find the answer to in the documentation. It's
>probably there, I'm just horrible at finding things like that.
Hello, sorry for the late reply... havn't checked my E-mai over the
weekend... I've seen several replies to this but figured an actual
solution that works with 1.5a and can be bound if necessary would work
better.... ;)
>procedure check_menu_input(integer x)
> if x < 1 or x > 2 then
> menu()
>-- The line above is the problem causing line. The interpreter says
>-- menu() has not yet been declared.
> elsif x = 1 then
> story1()
> elsif x = 2 then
> story2()
> end if
>end procedure
>
>function menu()
> -- Story menu
> printf(1, "\n\nPlease choose one:",0)
> printf(1, "\n1. A visit to the _",0)
> printf(1, "\n2. Dear Teacher",0)
> menu_num = waitkey(0)
> check_menu_input(menu_num)
>end function
Try changing the code to:
function check_menu_input(integer menu)
if menu = 1 then
story1()
elsif menu = 2 then
story2()
else
-- Bad input, setup menu to 0 and we'll loop again.
menu = 0
end if
return menu
end function
procedure menu()
-- Story menu
integer menu_num
menu_num = 0
while menu_num = 0 do
printf(1, "\n\nPlease choose one:",0)
printf(1, "\n1. A visit to the _",0)
printf(1, "\n2. Dear Teacher",0)
menu_num = waitkey(0)
menu_num = check_menu_input(menu_num)
end while
end function
I switched the procedure/function types because menu() wasn't returning
anything and it was appropriate for check_menu_input() to, so the loop
would work. Hope this helps. :)
_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]
6. Re: Newcomer with a problem
On Sun, 25 Jan 1998, David B. Williams wrote:
> Hello, everyone. I'm new to Euphoria, and have been having a problem
> that I can't seem to find the answer to in the documentation. It's
> probably there, I'm just horrible at finding things like that.
>
> I'm wondering about function and procedure declaration. Currently,
> I've got a function and a procedure. They both call the other at one
> point in their block, but because one appears before the other, the
> interpreter says the second one is undefined (and therefore will not
> continue running the program).
>
As people have already said, this is only possible in Euphoria 2.0 and up.
May I make a suggestion?
Yeah?
OK, here goes:
If development is ever going to continue from Eu1.5a, this is what I'd
like to see made possible:
type natural(integer x)
return x >= 0
end type
function odd({}) return {} end function
function even(natural a)
if a = 0 then
return 1
else
return not odd(a - 1) -- no error here, odd() has been
-- pseudo-declared
end if
end function
function odd(natural a)
if a = 1 then
return 1
else
return not even(a - 1)
end if
end function
While this is a horrible and unnecessary recursion, it's a good example of
what I'm after.
The {} in the first odd() argument list implies that odd() will be defined
later, and that any references to it before that time are OK.
The interpreter would have to spot if odd() wasn't defined later.
Also I wouldn't mind being able to create my own infix binary functions,
such as in Ada or Haskell:
function "xorb"(integer left, integer right) -- note the quotes
return xor_bits(left, right)
end function
? 170 xorb 255
--
Carl R White | e-mail...: crwhite- at -comp.brad.ac.uk
| finger...: crwhite- at -dcsun1.comp.brad.ac.uk
| web......: http://www.student.comp.brad.ac.uk/~crwhite/
*** Anti-Spam: Header tampered with. Change '- at -' to '@' above. ***