Function inside another function?... 
Author Message
 Function inside another function?...

Hi all!

I've got an old Pascal program to translate into C.
In pascal, we can easily write:
procedure a;
var
        myvar:integer;

        procedure b;
        begin
                blah blah...
        end;
begin
end;

The local variable "myvar" of procedure a is visible into the procedure
b.

How can I handle that in C?... I do not want to pass it as a parameter
because there are many variables like myvar and more, b is a recursive
procedure.

Any ideas better than:

int myvar;

void b()
{
        blah blah;

Quote:
};

void a()
{

Quote:
};

Thanks!
--
Jean-Yves TOUMIT.


http://www.*-*-*.com/



Tue, 08 Aug 2000 03:00:00 GMT  
 Function inside another function?...



Quote:
> Hi all!

> I've got an old pascal program to translate into C.
> In pascal, we can easily write:
> procedure a;
> var
>    myvar:integer;

>    procedure b;
>    begin
>            blah blah...
>    end;
> begin
> end;

> The local variable "myvar" of procedure a is visible into the procedure
> b.

> How can I handle that in C?... I do not want to pass it as a parameter
> because there are many variables like myvar and more, b is a recursive
> procedure.

> Any ideas better than:

> int myvar;

> void b()
> {
>    blah blah;
> };

> void a()
> {
> };

> Thanks!
> --
> Jean-Yves TOUMIT.


> http://rfv-pc28.insa-lyon.fr

Sorry, but no better ideas. C doesn't have any construction to
do such a thing. I think the best you can do is to put all
"global" information in a structure and pass a pointer each
time (don't pass the entire structure, that's to much):

struct global_data{
        int data;

Quote:
};

void b{global_data *d, int count)
{
        if (count)
                b(d, count-1);

Quote:
}

void a()
{
        global_data d;
        d.data = 10;
        b( &d, 10);

Quote:
}

Marco Schramp
----------------------------------------------------------------------------
-------
Working with a laptop on a plane causes a crash of the latter.


Tue, 08 Aug 2000 03:00:00 GMT  
 Function inside another function?...



Quote:
>Hi all!

>I've got an old pascal program to translate into C.
>In pascal, we can easily write:
>procedure a;
>var
>        myvar:integer;

>        procedure b;
>        begin
>                blah blah...
>        end;
>begin
>end;

>The local variable "myvar" of procedure a is visible into the procedure
>b.

>How can I handle that in C?...

This is one area that C doesn't handle that well.

Quote:
>I do not want to pass it as a parameter
>because there are many variables like myvar and more, b is a recursive
>procedure.

>Any ideas better than:

>int myvar;

>void b()
>{
>        blah blah;
>};

You don't want this last ; since compound statements { } aren't terminated
with a ;. Since you only want b to be visible to a (or as close to that
as possible) make b static.

Quote:
>void a()
>{
>};

You could do that unless you need the code to be thread-safe or reentrant.
Pascal is effectively giving you a link pointer behind the scenes, in C
you can create one explicitly. Since you have a number of variables you will
need to package them up in a struct so that they can all be referenced
through a single pointer:

typedef struct {
    int var1;
    double var2;
    /* and so on */

Quote:
} Vars_t;

static void b(Vars_t *vp);

void a(void)
{
    Vars_t vars;

    /* Set up any members that require it here using vars.var1 = ???; and
       so on */

    b(&vars);

Quote:
}

static void b(Vars_t *vp)
{
    /* You can access the members of vars in b() using vp->var1 and so on */

    b(vp);   /* Pass vp onto the recursive call */

Quote:
}

You can of course add other parameters as you wish to a() and b()

--
-----------------------------------------


-----------------------------------------



Tue, 08 Aug 2000 03:00:00 GMT  
 Function inside another function?...

What you can do is use #define macro.

#define b(p) do { \
  int v; \
  } while (0)

int a(void)
{
  int i;
....
     b(i);

Quote:
}

Or if you want to use non-ANSI, you can try inline or register parameter
passing(depending on your compiler)

Aloha



Tue, 08 Aug 2000 03:00:00 GMT  
 Function inside another function?...



Quote:
>Hi all!

>I've got an old pascal program to translate into C.
>In pascal, we can easily write:
>procedure a;
>var
>    myvar:integer;

>    procedure b;
>    begin
>            blah blah...
>    end;
>begin
>end;

You can't directly translate this to C; in C, all functions must be
at file scope. (The GNU C compiler does support functions within functions,
but this is a very non-portable extension.)

The idea in Pascal is that procedure b is only visible from within a,
and can only be legaly called from a. Furthermore, b can see and
manipulate the local variables in a. How this is implemented is at
the machine language level is, typically, using ``display'' pointers
in the stack frame.

To simulate the display mechanism in C, you can use pointers explicitly.
You gather the local variables of 'a' into a structure---or at least
those variables that are of interest to 'b'. You then pass into 'b'
a pointer to these locals.

        struct locals {
                int x;
                int y;
        }

        void b(struct locals *l);

        void a(void)
        {
                struct locals loc;
                b(&loc);
        }

        void b(struct locals *l)
        {
                l->y = 42;
                /* ... etc ... */
        }

Recursion is handled fine, because loc is an auto variable inside a; thus
different invocations of a can independently call b. In both the C and Pascal
version, the function 'b' is simply a subcontractor which performs some things
using locals within 'a'; it's just that in Pascal you have a special notation
geared toward expressing this relationship between a and b.

In the C program, anyone can call 'b', of course, provided they pass in
a valid pointer to a struct locals. It is easier to build a new variant
of function 'a' which directly reuses the 'b' code.

The function-within-function paradigm of Pascal is actually of little utility
in software development, though there are obvious instances where it
can come in handy.

Another tool that can help you is macro preprocessing.

        void a()
        {
                int x;
                #define b(X) do {                               \
                        int y;                                  \
                        /* something with X */                  \
                } while(0);

                b(x);
                #undef b
        }

This lets you create an function-call-like notation for some frequently
needed bit of code. Each instance of the notation is expanded into
program text that is in the same scope as the statement block in which
it occurs. You can call 'a' recursively within the body of this macro.
The backslashes are required, because they act as logical line
continuators, and a preprocessor directive such as a #define must
be written all on one logical line.

        The use of do/while(0) calls for some explanation.

        This construct ensures that the macro-expanded text is syntactically
        a statement, provided that a terminating semicolon is written after
        the macro invocation.  In C, the do { ... } while(0) trick is the
        only way to wrap some declarations and statements into their own
        block such that the resulting construct will require a trailing
        semicolon and consume it as part of its syntax, rather than treat it
        as an extra null statmement.  This lets you safely write
        something like

                if (condition)
                        b(x);
                else
                        foo = bar;

        Because b(x) expands to do..while(0), it represents one statement.
        If instead b(x) expanded into, say a simple block like { ... },
        then the trailing semicolon would introduce an extra null
        statement, and it would break the if/else statement; the
        else would be left dangling. The programmer would then have
        to write:

                if (condition)
                        b(x)    /* no semicolon */
                else
                        foo = bar;

        which breaks the syntax of the language, in violation of one
        of the ten commandments of C: ``thou shalt not use the
        preprocessor to change the syntax of the language''.



Tue, 08 Aug 2000 03:00:00 GMT  
 Function inside another function?...

Jean-Yves...

My Linux gcc compiler permits nested c functions and allows exactly
what you want to do.

Morris Dovey

Des Moines, Iowa, USA



Wed, 09 Aug 2000 03:00:00 GMT  
 Function inside another function?...


Quote:
>My Linux gcc compiler permits nested c functions and allows exactly
>what you want to do.

But, if you choose to write such code, it won't be Standard C.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Wed, 09 Aug 2000 03:00:00 GMT  
 Function inside another function?...



Quote:
>Jean-Yves...

>My Linux gcc compiler permits nested c functions and allows exactly
>what you want to do.

However the C language does not and it is uaully a bad idea to tie yourself
into a specific non-standard compiler (and gcc is non-standard when you
invoke it in a way that allows nexted function definitions).

--
-----------------------------------------


-----------------------------------------



Wed, 09 Aug 2000 03:00:00 GMT  
 Function inside another function?...



Quote:
>What you can do is use #define macro.

>#define b(p) do { \
>  int v; \
>  } while (0)

>int a(void)
>{
>  int i;
>....
>     b(i);
>}

>Or if you want to use non-ANSI, you can try inline or register parameter
>passing(depending on your compiler)

b() is specified as a recursive function, so this won't work.

--
-----------------------------------------


-----------------------------------------



Wed, 09 Aug 2000 03:00:00 GMT  
 Function inside another function?...


Quote:
(Lawrence Kirby) writes:
>b() is specified as a recursive function, so this won't work.

This is true if you just take multi-part questions literally, and narrow it to
yes or no answer.    Part of question was accessing a local
variable from another function without passing it, second part is
function in function, then recusive.   Also, this comes from a C newbie so
recursive could just mean 'called multiple times' (though most likely not.)
It will be shocking to me if _all_ of his functions were recusive, though
it is possible to have majority of function to be recursive in what I
consider 'elegant academic type' of programming.

Aloha



Thu, 10 Aug 2000 03:00:00 GMT  
 Function inside another function?...


Quote:

>Jean-Yves...

>My Linux gcc compiler permits nested c functions and allows exactly
>what you want to do.

Good for you! Now port your program to a compiler other than GCC!


Thu, 10 Aug 2000 03:00:00 GMT  
 Function inside another function?...



Quote:

>(Lawrence Kirby) writes:

>>b() is specified as a recursive function, so this won't work.

>This is true if you just take multi-part questions literally, and narrow it to
>yes or no answer.

I can find no indication that the original question was multi-part. The clear
statement was that b is a recursive procedure in the Pascal code and therefore
my comment that a macro version of it won't work.

Quote:
>Part of question was accessing a local
>variable from another function without passing it, second part is
>function in function, then recusive.

I think you should reread the question. It is certainly true that a macro
might do what's required in some contexts, I was just pointing out that it
won't work in the situation described by the original poster.

Quote:
>  Also, this comes from a C newbie so
>recursive could just mean 'called multiple times' (though most likely not.)

Recursion isn't just a C concept, it exists on its own in computer science
and an experienced Pascal programmer would certainly know about it. In fact
Pascal arguably supports recursion better than C does (as evidenced by the
question that started this thread).

Quote:
>It will be shocking to me if _all_ of his functions were recusive, though
>it is possible to have majority of function to be recursive in what I
>consider 'elegant academic type' of programming.

Sure, but the question was about how to solve a problem specifically when
the inner function is recursive. The issue tends to crop up most with
recursion because a recursive function often needs a wrapper function to
set up appropriate starting conditions for the recursion (and the
recursive function interface may not be the same as the entry function
interface.

--
-----------------------------------------


-----------------------------------------



Thu, 10 Aug 2000 03:00:00 GMT  
 Function inside another function?...

Read the subject line please.  You seem to think that something that nearly
works or that might work under some circumstances is just peachy keen.  That
solution does not address the needs of the original poster at all.  How would
you like a parachute that almost works?  Then, it turns out, it does not work
at all.
I tried to correct Lawrence once.  Turns out I had misread his answer, as I
recall.

--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Fri, 11 Aug 2000 03:00:00 GMT  
 Function inside another function?...


Quote:
(Lawrence Kirby) writes:

>I can find no indication that the original question was multi-part. The clear
>statement was that b is a recursive procedure in the Pascal code and
>therefore my comment that a macro version of it won't work.

I found that involved multi-part relating to parameter/function/ and recursion.

Quote:
>Recursion isn't just a C concept, it exists on its own in computer science
>and an experienced Pascal programmer would certainly know about it. In fact

I will change 'unlikely' to 'never', recursion wasn't refering to 'multiple
times.'

Quote:
>Sure, but the question was about how to solve a problem specifically when
>the inner function is recursive.

He needed to know other part too.  Though perhaps I could have mention
that #define was not for recursion, I thought "function in function" was main
part he wanted to know.

Also, I should have made made it clear that non-ANSI part  "was worse".

Quote:
>recursion because a recursive function often needs a wrapper function to

Yes

Quote:

>--
>-----------------------------------------


>-----------------------------------------

>------------------- Headers --------------------

>Path:
>lobby03.news.aol.com!newstf02.news.aol.com!portc04.blue.aol.com!nntprelay

.mathworks.com!woodstock.news.demon.net!demon!news.demon.co.uk!demon!mail2
news.demon.co.uk!genesis.demon.co.uk!fred

Quote:

>Newsgroups: comp.lang.c
>Subject: Re: Function inside another function?...
>Date: Sun, 22 Feb 98 21:39:25 GMT
>Organization: none





>X-Mail2News-Path: genesis.demon.co.uk
>X-Trace: mail2news.demon.co.uk 888191299 23466 fred genesis.demon.co.uk

>X-Newsreader: Demon Internet Simple News v1.30
>Lines: 48

Aloha


Fri, 11 Aug 2000 03:00:00 GMT  
 Function inside another function?...

Quote:
> Jean-Yves...
Hello!

> My Linux gcc compiler permits nested c functions and allows exactly
> what you want to do.

Well, as others pointed out, this is "gcc-specific" so for this one, I
won't
use this solution (it has to be compiled using Borland C++ Builder...).

Still, I'm interrested in this... How can you do it using gcc?... I
*have* gcc and
actually make the translation under linux! ;-) Still, I couldn't make it
work...
How do you do it? (give me a sample of your code)

Thanks. (This reply is for simple curiosity, I don't think I'll use it
because
it relies only on gcc but still, it can be interresting!)

--
Jean-Yves TOUMIT.


http://rfv-pc28.insa-lyon.fr



Fri, 11 Aug 2000 03:00:00 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. declaring functions inside other functions

2. Setting a pointer to a function, inside a function

3. printing function name and arguments from inside a function

4. function to be defined inside other function

5. Function pointer to function inside class

6. Call a function in a function with name of a function as parameter

7. functions, functions, and more functions!

8. How to use (pointer to function), and function and pointer to (pointer to function)

9. function pointer inside a class

10. Debugging Inside a Function

11. Allocating memory inside a function

12. malloc() inside functions

 

 
Powered by phpBB® Forum Software