Why can't a function be in a procedure?
Author |
Message |
Kharybdis
|
Posted: Fri Jan 04, 2008 6:09 pm Post subject: Why can't a function be in a procedure? |
|
|
My friend has made a slime game, and, in tribute to his slime game, i ask the question - in his slime game, a function cannot be in a procedure.
Is there any way to get past that? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: Fri Jan 04, 2008 6:19 pm Post subject: RE:Why can\'t a function be in a procedure? |
|
|
why make a function in a procedure.... how does that work
a function is a function and a procedure is a procedure, you make them separately, and call to them separately |
|
|
|
|
|
Tony
|
Posted: Fri Jan 04, 2008 6:43 pm Post subject: RE:Why can\'t a function be in a procedure? |
|
|
a procedure is a function that returns void. If your program calls for defining a function inside of another function, then something is very wrong. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Clayton
|
Posted: Sat Jan 05, 2008 1:08 pm Post subject: RE:Why can\'t a function be in a procedure? |
|
|
This concept is not entirely unheard of. However, not being an expert on the subject, a better person to ask would be wtd. |
|
|
|
|
|
TokenHerbz
|
Posted: Sat Jan 05, 2008 1:29 pm Post subject: Re: Why can't a function be in a procedure? |
|
|
Your asking if a function can be inside a procedure?
Quote: a function cannot be in a procedure.
Well it can, Here take a look!
code: |
var a, b : int %%number amounts
%%function that mutliplys the amounts
fcn Multiply (a_, b_ : int) : int
result a_ * b_
end Multiply
%%A PROC that DISPLAYS amounts
proc Display (a_, b_ : int)
put Multiply (a_, b_) %%calls a function inside a proc for a variable to display.
end Display
loop
get a, b %%user puts info
Display (a, b) %%calls a proc, which uses a fcn
Input.Pause
end loop
|
But you know, i don't think thats what you where asking was it???? |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sat Jan 05, 2008 3:50 pm Post subject: Re: RE:Why can\'t a function be in a procedure? |
|
|
Clayton @ Sat Jan 05, 2008 1:08 pm wrote: This concept is not entirely unheard of. However, not being an expert on the subject, a better person to ask would be wtd. If we are talking about defining functions/procedures inside another function/procedure, then there is no reason except that the language designers decided not to allow that. Indeed, GCC supports nested functions as an extension to C and Pascal has nested functions and procedures. |
|
|
|
|
|
Zampano
|
Posted: Sat Jan 05, 2008 5:32 pm Post subject: Re: Why can't a function be in a procedure? |
|
|
Wasn't it that a function can be anywhere, except that when a function or procedure is placed within another, the function or procedure that is being called in the other has to have been declared at an earlier stage is the program? If the multiply function in your example is placed after the procedure, it will fail. As long as the function has been declared beforehand, you can call them arbitrarily.
The rule of having an object (is that the right word?) known at compile is common. It stops people from doing stuff like records that have themselves as source or something . . . I think. |
|
|
|
|
|
Tony
|
Posted: Sat Jan 05, 2008 8:42 pm Post subject: Re: Why can't a function be in a procedure? |
|
|
Zampano @ Sat Jan 05, 2008 5:32 pm wrote: the function or procedure that is being called in the other has to have been declared at an earlier stage is the program?
Well you could forward your functions, so that's not really a problem.
My concern has to do with the scope. If a function is defined within another function, former will have a scope, local to the latter. Maybe there's a practical counter-example to this, but I would think that if a single function is large enough to require a local function just for itself, then the design could be refactord. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Sponsor Sponsor
|
|
|
ericfourfour
|
Posted: Sat Jan 05, 2008 8:52 pm Post subject: Re: Why can't a function be in a procedure? |
|
|
I haven't posted here in a while, but I thought I'd help out with this one.
I can't really thing of a scenario where one would define a function inside a procedure but there are a few alternatives to this. One is if you want to introduce another scope within the procedure. The other is if you want to use a certain namespace.
If you want to introduce another scope, use the begin/end commands.
Turing: | proc foobar
% This is foobar's scope. Variables from foobar's scope can only be
% accessed in foobar.
var foo : string := "foo"
% Variables declared in the begin scope, cannot be accessout outside it.
begin
var bar : string := "bar"
put foo + bar
end
put foo
end foobar
foobar |
If you want to add a namespace look into modules. The purpose of modules in Turing is to add a namespace.
Turing: | module Foo
export bar
proc bar
put "foobar"
end bar
end Foo
Foo.bar |
You can even have nested modules.
Turing: | module Foo
export Bar
module Bar
export foobar
fcn foobar : string
result "foobar"
end foobar
end Bar
end Foo
put Foo.Bar.foobar |
It should be noted that calling procedures from a nested module will cause a run-time warning. The program will still run however. Functions do not cause this problem. |
|
|
|
|
|
Kharybdis
|
Posted: Sun Jan 06, 2008 11:50 am Post subject: Re: Why can't a function be in a procedure? |
|
|
Thank you everyone
|
|
|
|
|
|
|
|