Bloomberg LP Interview Question
Financial Software DevelopersActually it depends on whether you use the function or not. This will compile with both statements because the local declaration shadows the scope of the external declaration. However, if the function is actually used, there has to be an actual definition of the function somewhere. Neither of these two statements provide such a definition.
When extern is put in front of a variable it means that the memory for the variable is some where else.When extern is put before a function, what exactly does this mean ?That the function body is some where else ??
extern in front of a function prototype is a no-op (it makes no difference); it's all just declaration (apart from 'extern "C" ' forms, that is). extern makes a difference for variables only.
int x; // defined, ie allocated space, here.
extern int y; // declared here - we know there's a y that's an int that we can expect at link time.
// this link says as much, by its silence. I've found the site reliable.
***.cppreference.com/wiki/keywords/extern
And, you can have as many declarations of the same function as you like (for convenience with header files) so, having 2, one with extern and one without really makes no difference.
dfmcla's Feb 28: "This will compile with both statements because the local declaration shadows the scope of the external declaration." - no shadowing happens, the 2nd is just an extra declaration. The compiler will nod, shrug and continue.
extern void asprint(int *ia, int sz);
void asprint(int *ia, int sz); // okay, compiler ignores the word "external", i.e.it does not make any sense for function definitions
void asprint(int *ia, int sz);
void asprint(int *ia, int sz); // okay, we ignore the second definition, as it it the same as the first one
void asprint(float *ia, int sz);
void asprint(int *ia, int sz); // still okay, function overloading
int asprint(int *ia, int sz);
void asprint(int *ia, int sz); // OOPS! function redefinition with different returning type! Error!
why does Bloomberg ask these stupid questions which have no use in practical life.
- NK October 02, 2011