Jump to content
  • Advertisement
Sign in to follow this  
Lode

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Erzengeldeslichtes
Do 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 this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
Quote:
Original post by Erzengeldeslichtes
Do 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Lode
Is 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 this post


Link to post
Share on other sites
Quote:
Original post by Decibit
It'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 this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Nested 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 ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!