Function definition nesting

  • Thread starter =?iso-8859-1?Q?Jos=E9?= de Paula
  • Start date
?

=?iso-8859-1?Q?Jos=E9?= de Paula

Does C99 support defining a function in the body of another function?
I mean, something like:

int a (void)
{
int x;
int y;

int b(int c)
{
/* function b definition here */
};

/* body of function a */
}
 
M

Malcolm

José de Paula said:
Does C99 support defining a function in the body of another
function?
No. You can get most of the functionality of local functions by placing your
function in a file of its own, and then declaring the sub-functions as
static functions in the same file.
 
A

Alexander Bartolich

begin followup to Malcolm:
No. You can get most of the functionality of local functions by
placing your function in a file of its own, and then declaring
the sub-functions as static functions in the same file.

No, that's not what nested functions are about.
They have access to the local variables of their parent functions.

procedure a;
var x:integer;

procedure b;
begin
writeln(x);
end;

begin
x := 1; b;
x := 2; b;
end;

Experienced programmers can write Fortran in any language.
And any means.
 
T

Tom St Denis

Alexander said:
begin followup to Malcolm:

No, that's not what nested functions are about.
They have access to the local variables of their parent functions.

so you make

int test(void)
{
int a, b;

int test2(void) { return a + b; }

a = 4; b = 5;
return test2() + 3;
}

can be turned into
 
K

Keith Thompson

Tom St Denis said:
so you make

int test(void)
{
int a, b;

int test2(void) { return a + b; }

a = 4; b = 5;
return test2() + 3;
}

can be turned into

---
static int a, b;
static int test2(void) { return a + b; }
int test(void) { a = 4; b = 5; return test2() + 3; }
---

That's not the same thing at all. In the nested-function version
(which is not valid C), a and b are local variables, so each
invocation of test() gets its own unique copy of each of them. In
your static version, the lifetime of a and b is the entire execution
of the program; recursive calls to test() will share the same copies.

If your goal is to create a function that does the same thing as the
original one, you might as well write

int test(void)
{
return 12;
}

If you want to write something that duplicates the semantics of nested
functions with access to the parent's local variables, it's possible
(though probably not worth the effort), but making the local variables
static isn't the way to do it.
 
T

Tom St Denis

Keith said:
That's not the same thing at all. In the nested-function version
(which is not valid C), a and b are local variables, so each
invocation of test() gets its own unique copy of each of them.

I'd say C doesn't know about "threads" so really the point is flexible.
Given the only entry-point is test() the implementation is comparable.

Tom
 
M

Mark McIntyre

Keith Thompson wrote: (re nested fn vs static fn)

I'd say C doesn't know about "threads" so really the point is flexible.

Where did threads come from? If you call test() recursively, or even
iteratively, each iteration of the nested version gets a unique copy of the
locals, whereas the static version doesn't.
 
C

CBFalconer

Tom said:
so you make

int test(void)
{
int a, b;

int test2(void) { return a + b; }

a = 4; b = 5;
return test2() + 3;
}

can be turned into

---
static int a, b;
static int test2(void) { return a + b; }
int test(void) { a = 4; b = 5; return test2() + 3; }
---

I'm glad you think so. You are wrong if you consider that a
general transcription method. Consider recursive calls to test.
 
T

Tom St Denis

CBFalconer said:
I'm glad you think so. You are wrong if you consider that a
general transcription method. Consider recursive calls to test.

Meh... ok consider it a slightly similar implementation of it. Given that
the former is not valid C it's upto .... SCAPEGOAT.

Ah shut up y'all. I was just making talk.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top