# Function in function

This topic is 5021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Wouldn't it be handy if you could do something like this?
int whisky(int a, int b)
{
int c = a * b;

int tango(int x);
int foxtrot(int x);

c = tango(a + c) * foxtrot(b);

return c;

int tango(int x)
{
return x * a;
}

int foxtrot(int x)
{
return tango(a + b + c + x);
}
}

A function inside a function allows you to do things with variables in your big function, without having to make them global! Is there a language that can do this?

##### Share on other sites
Isn't that what parameters are for?

##### Share on other sites
You could always use c/c++, and more specifically, the define macro.

int whisky(int a, int b){  int c = a * b;    #define tango(x)     (x * a)    #define foxtrot(x)     tango(a + b + c + x)    c = tango(a + c) * foxtrot(b);    #undef tango  #undef foxtrot    return c;}

##### Share on other sites
Do this in C++:
int whisky(int a, int b){    int c = a * b;class InnerFunc{    int &a, &b, &c;public:    InnerFunc(int& a, int& b, int& c) : a(a), b(b), c(b) {}    int tango(int x)    {        return x * a;    }    int foxtrot(int x)    {        return tango(a + b + c + x);    }} InFunc(a, b, c);    c = InFunc.tango(a + c) * InFunc.foxtrot(b);    return c;}

You are allowed to define a class or structure within a function, so you can "cheat" the fact that you can't define a function within a function using a class that you define in your function. You'll still have to pass in the referances to the variables, it won't have access to the parent function's variables otherwise. Using Gorax's method you don't have to pass in the variables, but it is extremely unsafe. (at least put parenthesis around x! (that is: "#define tango(x) ((x)*a)" ))

[Edited by - Erzengeldeslichtes on January 18, 2005 8:04:43 AM]

##### Share on other sites
Quote:
 Original post by ErzengeldeslichtesDo this in C++:*** Source Snippet Removed ***You are allowed to define a class or structure within a function, so you can "cheat" the fact that you can't define a function within a function using a class that you define in your function. You'll still have to pass in the referances to the variables, it won't have access to the parent function's variables otherwise.

If you only need one function, then (using this inner class idea) you should use:
void func(type1 param1, type2 param2){   struct innerfunc   {     void operator()(type1 param1, type2 param2)     {       // do something     }   } my_innerfunc;   my_innerfunc(param1, param2); // really looks like an "internal" function call}

HTH,

##### Share on other sites
Quote:
Original post by Emmanuel Deloget
Quote:
 Original post by ErzengeldeslichtesDo this in C++:*** Source Snippet Removed ***You are allowed to define a class or structure within a function, so you can "cheat" the fact that you can't define a function within a function using a class that you define in your function. You'll still have to pass in the referances to the variables, it won't have access to the parent function's variables otherwise.

If you only need one function, then (using this inner class idea) you should use:
void func(type1 param1, type2 param2){   struct innerfunc   {     void operator()(type1 param1, type2 param2)     {       // do something     }   } my_innerfunc;   my_innerfunc(param1, param2); // really looks like an "internal" function call}

HTH,

Ooh, good idea Emmanuel.
The big problem with this method is that you can't access data or functions in the "parent" function.

[Edited by - Erzengeldeslichtes on January 18, 2005 8:28:36 AM]

##### Share on other sites
Pascal allows you to use such functions and treats variables exactly the way you've described here. They are called 'nested' functions.

Nested functions are quite common. They can be easily realized on machine level. x86 assemly language even has two special instructions: ENTER and LEAVE that support this conception in hardware.

It's a matter of language design that C/C++ doesn't have nested functions. They provide no computational benefit and were not included.

##### Share on other sites
Quote:
 Original post by LodeIs there a language that can do this?

Python and any Pascal variant seem to support this natively. There may be more languages, but I can't think of them right now [smile]

##### Share on other sites
Quote:
 Original post by DecibitIt's a matter of language design that C/C++ doesn't have nested functions. They provide no computational benefit and were not included.

Nested functions are legal in C, but illegal in C++.

##### Share on other sites
Quote:
 Original post by FrunyNested functions are legal in C, but illegal in C++.

Maybe. But MSVC++ NET 2003 doesn't compile them even when specifically told to compile files as C code sources. (/TC option).

What C do you mean: ANCI C or K&R C ?

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632936
• Total Posts
3009311
• ### Who's Online (See full list)

There are no registered users currently online

×