Valhalla Legends Forums Archive | General Programming | In C, how do I...

AuthorMessageTime
tA-Kane
...make a function only accessable by another function?

I tried doing it this way, but my compiler gave me the error "Illegal Function Definition"[code]void a(void);
void a(void){
void b(void);
void b(void){
//code
}
//code, referencing b
}[/code]Since this is an "Illegal Function Definition", where does the function definition go?
May 13, 2003, 11:49 PM
Zakath
Why do you need to create another function? Wouldn't it be easier simply to have a contain all the code? Building a function with local scope inside another function just doesn't seem to make any sense to me, maybe I'm missing something.
May 14, 2003, 12:00 AM
tA-Kane
[quote author=Zakath link=board=5;threadid=1328;start=0#msg9903 date=1052870412]Why do you need to create another function? Wouldn't it be easier simply to have a contain all the code? Building a function with local scope inside another function just doesn't seem to make any sense to me, maybe I'm missing something.[/quote]It's mostly for readability. Dumb thing, eh? Oh well. But, it's always good to know how it could be done, if there's ever a legitimate reason..., is it not?
May 14, 2003, 12:03 AM
Eibro
[quote author=tA-Kane link=board=5;threadid=1328;start=0#msg9902 date=1052869788]
...make a function only accessable by another function?

I tried doing it this way, but my compiler gave me the error "Illegal Function Definition"[code]void a(void);
void a(void){
void b(void);
void b(void){
//code
}
//code, referencing b
}[/code]Since this is an "Illegal Function Definition", where does the function definition go?
[/quote]Simple, you can't. The function definition goes outside any other function definition, at global scope. Although C++ does have the facility to do such a thing.
May 14, 2003, 12:33 AM
tA-Kane
[quote author=Eibro link=board=5;threadid=1328;start=0#msg9908 date=1052872401]Simple, you can't. The function definition goes outside any other function definition, at global scope.[/quote]Hrmmm... bah... OK.
May 14, 2003, 12:44 AM
Kp
[quote author=Eibro link=board=5;threadid=1328;start=0#msg9908 date=1052872401]Simple, you can't. The function definition goes outside any other function definition, at global scope. Although C++ does have the facility to do such a thing.[/quote]Not entirely true. gcc permits this, and uses exactly the syntax Kane posted. The primary advantage of such a construct is that the inner function knows and can reference all variables owned by the parent function, provided said variables were declared before the inner function. However, this is a language extension which (afaik) works only in gcc.
May 14, 2003, 3:35 AM
MrRaza
In C, all functions are at the same scope level. That is, you cannot define a function within a function. This is why C is not technically a block-structured language.
Look up the Scope Rules of Functions.
May 14, 2003, 3:54 AM
MrRaza
I'll elaborate on what i stated. Basically, in C, each function is a discrete block of code. A Function's code is private to that function and cannot be accessed by any statement in any other function except through a call to that function. The code that constitutes the body of a function is hidden from the rest of the program and, unless it uses global variables or data, it can neither affect or be affected by other parts of the program. Stated another way, the code and data that are defined within one function cannot interact with the code of data defined in another function because the two functions have different scope.
May 14, 2003, 12:25 PM
Yoni
Surprisingly, nobody said this: Use static.
[code]/* somefunction.c */
static void helper() { /* do stuff */ }
void somefunction() { /* do stuff */ }

/* somefunction.h */
void somefunction();[/code]

somefunction can be called by any source file that includes somefunction.h (and in C, also by ones that don't). helper can only be called in somefunction.c, because it is static there.
May 14, 2003, 1:47 PM
Kp
[quote author=Yoni link=board=5;threadid=1328;start=0#msg9947 date=1052920049]
Surprisingly, nobody said this: Use static.[/quote]Suggesting a *standards-compliant* way to do this?? For shame! ;)

I posted my (compiler specific) solution thinking Kane wanted to be able to access the parent's stack variables, though he never actually said so.
May 14, 2003, 5:02 PM

Search