• Welcome to Valhalla Legends Archive.
 

In C, how do I...

Started by tA-Kane, May 13, 2003, 06:49 PM

Previous topic - Next topic

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"void a(void);
void a(void){
 void b(void);
 void b(void){
   //code
 }
 //code, referencing b
}
Since this is an "Illegal Function Definition", where does the function definition go?
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

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.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

tA-Kane

Quote from: Zakath on May 13, 2003, 07:00 PMWhy 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.
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?
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

Eibro

Quote from: tA-Kane on May 13, 2003, 06:49 PM
...make a function only accessable by another function?

I tried doing it this way, but my compiler gave me the error "Illegal Function Definition"void a(void);
void a(void){
 void b(void);
 void b(void){
   //code
 }
 //code, referencing b
}
Since this is an "Illegal Function Definition", where does the function definition go?
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.
Eibro of Yeti Lovers.

tA-Kane

Quote from: Eibro on May 13, 2003, 07:33 PMSimple, you can't. The function definition goes outside any other function definition, at global scope.
Hrmmm... bah... OK.
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

Kp

Quote from: Eibro on May 13, 2003, 07:33 PMSimple, 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.
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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

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.

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.

Yoni

Surprisingly, nobody said this: Use static.
/* somefunction.c */
static void helper() { /* do stuff */ }
void somefunction() { /* do stuff */ }

/* somefunction.h */
void somefunction();


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.

Kp

Quote from: Yoni on May 14, 2003, 08:47 AM
Surprisingly, nobody said this: Use static.
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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!