Functions declared within a function in C are they usable at a global scope

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I ve been running into a minor problem with regards to seeing people, currently helping a teacher out through grading, and seeing people declaring functions within the scope of main, instead of within the global scope, as I was taught to do, I understand that this still works in itself, but I am kind of confused as to what happens with these functions, should a function be called, that was declared in main, in another function that is not declared inside said main, would this in itself work, and if it does, why. Isn t the function within only the scope of main, and as such shouldn t it remain local to it, or dies C just allocate the space in memory for the functions call at a global scope from the time it is declared, and subsequently defined? Know I was rambling a bit on this, trying to cover all possible questions within this scope. Sample of what I mean.

int main(){  
  void foo(int bar);
  int boo;
  foo(boo);
  return 0;
}

void foo(int bar){
  ...
}

The point that I was trying to stress is that the functions are usable due to the fact that they are related, through main if that makes any sense, but should a function be declared on the global scope such as

#include <stdio.h>

void roo(void);

int main(){} //Holds the same syntax as above with the function declared within it.
//Function foo is same as defined prior.
//Then function roo is defined but uses foo within itself.

void roo(void){
int boo;
foo(boo);
...
}

Shouldn t the codes being compounded, not work fully since the function roo technically has no way to access the function within the scope of main?

edit --

I somewhat saw a point that main would be, with the current lack of multiple functions to be called, would still be at the time used to call function roo, but should this contain multiple functions then wouldn t it be kind of weird or not syntax legal to call up foo from within roo, if it were scaled about 6 functions outside from main.

Answers

Declarations are needed for type checking and linking. They exist at compile-time only - they don t actually create an "object" (such as a function body) that exists in any way in the compiled program - all they do is put a name on it, so that other parts of the source can refer to that object without having it immediately to hand. Definitions create objects, but declarations create names.

Going through your example:

void roo(void);  // roo becomes visible

int main(){  // main is defined, AND becomes visible
    void foo(int bar);  // foo becomes visible
    foo(6);
    roo();
}  // foo is no longer visible

void foo(int bar){ /**/ }  // foo is defined AND becomes visible

void roo(void){  // roo is defined, but was already visible
    foo(6);      //  so this has no effect on other functions
}
    • roo can be called from any code in the program below where it was declared, because its name has been put in global scope and is visible from all nested scopes. The following code is aware that somewhere (no idea where), a function called roo exists and is available for use.
    • Moving down into main, a new scope is opened, and foo made visible within it. main uses this information to call to foo. It uses the information in the global scope to call to roo. The scope closes with the end of main and foo ceases to be visible to the following code; it is not aware that any such function exists.
    • foo is the simultaneously defined and declared in the global scope. All following code is aware that a function called foo is available for use.
    • Similarly for roo.

The only places anything is given space in memory are at the three points of definition . None of the declarations cause anything to be allocated, statically or otherwise. All they do is make the code aware of an allocation elsewhere (to be resolved by the linker).


So to (what I think was) the original point of confusion: main calls roo, and roo calls foo, but the declaration of foo in main is not visible to roo. (It sounds like you re confusing static and dynamic semantics: C has static scope only.)

This isn t a problem, because main doesn t know the content of roo, and doesn t need to know its content (some compilers perform inlining; we don t care about that). Control leaves the body code of main and goes to a different point in the program. The body of a function is a black box to every other function; what happens within the code of roo is completely unknown to every other function. roo also doesn t inherit the scope of main, so it has no knowledge of anything that was declared at that time (globally, locally, or otherwise) - it uses the scope opened and closed by its own body further down for name lookups. In that scope, foo has already been declared globally (it inherits the global scope at a separate fixed point on the page), so roo can see foo of its own accord.

Each function exists in one (and exactly one) place. All declarations do is make that place visible to the code that uses it. Code that doesn t use it, doesn t need to be able to see it. main can t see the nested call to foo, so any declaration at that point isn t relevant to it.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/26786653/functions-declared-within-a-function-in-c-are-they-usable-at-a-global-scope

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils