473,413 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Function definition nesting

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 */
}

--
Quidquid latine dictum sit altum viditur
Nov 14 '05 #1
9 2162
José de Paula wrote:
Does C99 support defining a function in the body of another function?


No
Nov 14 '05 #2

"José de Paula" <jo***********@ig.com.br> wrote in message
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.
Nov 14 '05 #3
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.

--
Für Google, Tux und GPL!
Nov 14 '05 #4
Alexander Bartolich wrote:
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.


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; }
---

Tom

Nov 14 '05 #5
Tom St Denis <to*@securescience.net> writes:
Alexander Bartolich wrote:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #6
Keith Thompson wrote:
Tom St Denis <to*@securescience.net> writes:
Alexander Bartolich wrote:
> 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.


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.


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
Nov 14 '05 #7
Tom St Denis wrote:
Alexander Bartolich wrote:
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.


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.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #8
On Mon, 05 Apr 2004 00:13:58 GMT, in comp.lang.c , Tom St Denis
<to*@securescience.net> wrote:
Keith Thompson wrote:

(re nested fn vs static fn)

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.


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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #9
CBFalconer wrote:
Tom St Denis wrote:
Alexander Bartolich wrote:
> 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.


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.


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
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
10
by: Phil Reardon | last post by:
Ive been away from programming for a few years and am having difficulty accessing a function from a math/engineering library that I want to use . I thought that double foo(double); inserted in the...
18
by: José de Paula | last post by:
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) {
38
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
2
by: kaushikroy | last post by:
i m getting this error when i m executing a stored procedure Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32). can anyone help me out what exactly the...
21
by: MAx | last post by:
Hi, Could any one list possible number of diferences between a function and a macro? which one will be faster and why?? ex : function and a macro ti find max of two nums #define MAX(a,b) (a>b)...
2
by: Johannes Bauer | last post by:
Nick Keighley schrieb: Why is there actually a *need* for nested functions? If functionality of subfunctions which are only locally visible is desired, why not put the nesting function parent...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.