subfunctions?

Member
Posts: 749
Joined: 2003.01
Post: #1
Say I have a function with some parameters, and I want this function to call some other function that is used exclusively inside this function and takes the same parameters, just to make things more tidy.

For instance

function (a,b,c)
{
for i=1 to 10
{
subfunction(a,b,c)
}
}


Is there a way (say in c++) to specify that subfunction is a "subfunction" of function, hence it takes the same parameters? or would I have to declare it in the usual way and just pass the parameters like above?


I was thinking of something like

function (a,b,c)
{
for i=1 to 10
{
subfunction()
}
}

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #2
As far as I know, this is not possible, but you can simply declare the function inside your main function, like:

function (a,b,c)
{
subfunction(a, b, c)
{
// do subfunction stuff
}

for i=1 to 10
{
subfunction(a,b,c);
}

}
Quote this message in a reply
Jordan
Unregistered
 
Post: #3
In the C language variables declared inside a function are limited in scope to the function. So, subfunction() nested inside function(a,b,c) will have no awareness of variables a, b or c.

You could either make subfunction() take arguments like your first example or make those variables global so both function() and subfunction() would have access to them.

In Objective-C, if a, b and c were variables in a class, all methods belonging to that class would have access to those variables. Methods of the same class can be nested in other methods and know about the class variables without the need to make the variables in those methods global or pass them as arguments.

As for C++, I don't know that language.
Quote this message in a reply
Member
Posts: 40
Joined: 2004.12
Post: #4
I'm thinking that a functor could do what you want if you have your "subfunction" become a sort of subfunctor, inheriting from the parent functor. I have never tried this nor do I know if it would work, but thats what I'd try.

http://gethelp.devx.com/techtips/cpp_pro...in0100.asp
http://www.sgi.com/tech/stl/functors.html
http://cplus.about.com/od/stltutorial/l/aa121503a.htm

HTH

Jericho
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #5
Cochrane Wrote:As far as I know, this is not possible, but you can simply declare the function inside your main function, ...

that's the thing you can't do. it doesn't let you declare a function inside the scope of another function. (otherwise he wouldn't need to pass the parameters.)

you could always write a macro as a subfunction but that probably wouldn't be a wise thing to do unless it does something really trivial.
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #6
Cochrane Wrote:As far as I know, this is not possible, but you can simply declare the function inside your main function, like:
Code:
function (a,b,c) {
   subfunction(a, b, c) {
      // do subfunction stuff
   }

   for (i = 1; i < 10; i++) {
      subfunction(a,b,c);
   }
}
Actually this is not possible in C and C++. I would hazard a guess that it's not possible in Obj-C either. All functions need to be declared at the file level. It can't be found inside another function. Either that or it's some new non-standard way that is available now that I've never seen or will ever use.

Pascal and one or two other languages did allow this from what I recall.

Najdorf:
C/C++/Obj-C is simple. You have to be explicite. Besides, how would someone know that a, b and c are the values that get passed to the function where you pass nothing? How would the compiler when it is possible in C++ (Obj-C as well?) to over load the function name with different arguments. I.E.
Code:
/* version a */
int myFunctionName(int a, int b) {...}
/* version b */
int myFunctionName(double a, double b) {...}
...
double dval=200.;
int       ival=20;
myFunctionName(dval, dval);
myFunctionName(ival, ival);
In C++ this is legal and the compiler is smart enough to call the right function depending on the arguement list.

So basically, from the compiler standpoint, if you had function() with no arguments, the compiler will hunt down a function definition with no arguments, not a function definition with three or how many arguments.
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #7
Well, the following code works fine for me:
Code:
- (void)prepareRendering
{
    int xoffset, yoffset;
    inline float height(int x, int y)
    {
        int mx = x + xoffset;
        int my = y + yoffset;
        if (mx >= maxX) mx = 0;
        else if (mx < 0) mx = maxX - 1;
        if (my >= maxY) my = 0;
        else if (my < 0) my = maxY - 1;
        return heightpointer[my * maxX + mx];
    }
    inline float *normal(int x, int y)
    {
        int mx = x + xoffset;
        int my = y + yoffset;
        if (mx >= maxX) mx = 0;
        else if (mx < 0) mx = maxX - 1;
        if (my >= maxY) my = 0;
        else if (my < 0) my = maxY - 1;
        return &normalpointer[(my * maxX + mx) * 3];
    }

...

vector_i[0][1] += height(x + xi, y + yi);

...

(do not mind what's actually in this functions)

It worked just the same when I used plain C, but I only made some trial and error to see if it works and compiles (it does), so I have no idea whether this works with all compilers or if I do not declare the functions inline.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
It's a GCC extension, and doesn't work in C++:

http://gcc.gnu.org/onlinedocs/gcc-4.0.2/...-Functions
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #9
Cochrane Wrote:Well, the following code works fine for me:
Code:
- (void)prepareRendering
{
    int xoffset, yoffset;
    inline float height(int x, int y) {...}
    inline float *normal(int x, int y) {...}
...
vector_i[0][1] += height(x + xi, y + yi);
...

(do not mind what's actually in this functions)

It worked just the same when I used plain C, but I only made some trial and error to see if it works and compiles (it does), so I have no idea whether this works with all compilers or if I do not declare the functions inline.
Blink Which compiler are you using? Does it work without the 'inline' tag? Are you sure it was compiling the 'C' code with a 'C' compiler or compiling the 'C' code with 'C++' compiler?

I just did a small test on a windows box with MS's compiler and it complains with an error "Local function definions are illegal" so this is not portable coding. Older compilers would probably choke on this code too so it must be something new with the newer GCC compilers. I'd use caution coding this way.

Edit:
OSC:
Thanks!
Quote this message in a reply
Post Reply