Board index » delphi » why define a function w/in another function?

why define a function w/in another function?

I'm learning c++ and Java, and I was wondering why you Pascal programmers
define a procedure w/in another procedure, but the inner procedure is
not accessible outside the outer procedure.  What does that do for you?

--J

 

Re:why define a function w/in another function?


Quote
john wrote...

>I'm learning c++ and Java, and I was wondering why you Pascal programmers
>define a procedure w/in another procedure, but the inner procedure is
>not accessible outside the outer procedure.  What does that do for you?

I am no expert Pascal programmer. But allow me to make an example.
A nested Pascal procedure is like a bathroom door within a highrise unit.
Only the highrise unit owner(s) can access the bathroom door.
People in other highrise units can access the public pool (and the like),
but not a particular owner's bathroom.

It make sense to me. Does it make sense to C programmers?
Do you "C" people always go camping and use only public bathrooms? :)

Re:why define a function w/in another function?


john schrieb:

Quote

> I'm learning c++ and Java, and I was wondering why you Pascal programmers
> define a procedure w/in another procedure, but the inner procedure is
> not accessible outside the outer procedure.  What does that do for you?

It has to do with CLEAN structured programming. Especially if
you have several people work on one project, it could easily
happen that there come out various procedures with the same
name. There are 2 approaches to make the troubles small, one
is the nested method and another is the UNIT's approach.

Experienced programmers know that in 2 years, when maintenance
of a program starts, the procedure names are forgotten. So it
saves a lot of troubles to "hide" the local procedures as good
as possible.

And a very useful benefit is the availability of the local
variables in the inner procedures.

A very typical example is an error handling procedure, made as
a function for multiple use:

Procedure OuterProc(A_parameter : Real; Another_P : Integer; FN:String);
Var A, B : Integer;  {could be referred in the local proc too!}
    F    : File;
  Function ErrExit(Cond : Boolean; Errmsg:String):Boolean;
  Begin
    ErrExit := Cond; {seems silly, but is NOT!}
    if Cond then
      Writeln('Error: ',Errmsg);
  End;
Begin
  if ErrExit(FN='','no filename specified') then Exit;
  Assign(F,FN);
{$I-}
  Reset(F,1);
{$I+}
  if ErrExit(IOresult <> 0,'with File '+FN) then Exit;
  ....
  if ErrExit(haveread <> size,'reading File '+FN) then Exit;  
....
End;

Visit:
http://www.geocities.com/SiliconValley/2926/tp.html

Franz Glaser

Re:why define a function w/in another function?


Sorry, this was a rather non-brillant example. I forgot the
main issue during writing. It should be easily understandable,
but all the reduction made it useless. This is a better
example:

Procedure OuterProc(A_parameter : Real; Another_P : Integer; FN:String);
Var A, B, size : Integer;  {could be referred to in the local proc too!}
    FileIsOpen:Boolean;
    F    : File;

  Function ErrExit(Cond : Boolean; Errmsg:String):Boolean;
  Begin
    ErrExit := Cond; {seems silly, but is NOT!}
    if Cond then
      Begin
        Writeln('Error with ',FN);
        Writeln(Errmsg);
        if FileIsOpen then
          Close(F);
      End;
  End;
Begin
  FileIsOpen := false;
  if ErrExit(FN='','no filename specified') then Exit;
  Assign(F,FN);
{$I-}
  Reset(F,1);
{$I+}    
  Case IOresult of
 0 : FileIsOpen := true;
 2 : if ErrExit(true,'File Not Found') then Exit;
 4 : if ErrExit(true,'Directory Not Found') then Exit;
 5 : if ErrExit(true,'Access Forbidden') then Exit;
 else
   if ErrExit(true,'Undefined Error opening') then Exit;
 End;
....
  BlockRead(...
  if ErrExit(haveread <> size,'Reading File') then Exit;  
....
End;

Franz Glaser

Re:why define a function w/in another function?


Quote
>john wrote...
>>I'm learning c++ and Java, and I was wondering why you Pascal programmers
>>define a procedure w/in another procedure, but the inner procedure is
>>not accessible outside the outer procedure.  What does that do for you?

Mason Ip replied...

Quote
>I am no expert Pascal programmer. But allow me to make an example.
>A nested Pascal procedure is like a bathroom door within a highrise unit.
>Only the highrise unit owner(s) can access the bathroom door.
>People in other highrise units can access the public pool (and the like),
>but not a particular owner's bathroom.

This is a good example.  The word you're searching for is
"encapsulation".

Each symbol in a Pascal program has a "scope" within which
the symbol is available, and outside of which the symbol isn't
available.  Nesting a procedure or function inside another function
allows you to restrict the scope of that procedure or function to
something appropriate for it.

In fact, a large percentage of the Pascal procedures and functions
I have written began their lives as nested procedures:

I frequently found identical sequences of statements sprinkled 3 or 4
times throughout a complicated procedure or function.
It was easy to turn such a sequence of statements into a parameterless
nested procedure.

In addition, really long procedures or functions can be broken into a few
"phases" of operation, which become nested procedures or functions.
The outside function body is replaced entirely with calls to the nested
procedures and functions.

Quote
>It make sense to me. Does it make sense to C programmers?
>Do you "C" people always go camping and use only public bathrooms? :)

Essentially, yes!

C has no concept of encapsulation beyond the idea of a "Translation Unit"
(essentially separate source files; that is, the public bathrooms in Japan
aren't available if you're in Iceland).

This was a major impetus behind the development of C++.  Local functions
are the Pascal feature I miss the most in C++.

Hope this helps,
Spencer

Other Threads