C/C++ question - Function prototype in other function
-
Hello, Pardon me, its a pure question of C/C++ not specific to Visual C++. Can anybody explain what is the purpose of following code: void Function1(int a, int b) { void Function2(int c, int d, int e); // in some header file void Function3(int c, int d, int e); // in some header file int i, k, l; //rest of the code in which Function2 and Function3 are being used! } I have no idea why "Function2" & "Function3" prototypes are defined in the function named "Function1", and is it possible? If yes whats the logic behind it? Any help is highly appreciated. Regards, Adeel
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
-
Hello, Pardon me, its a pure question of C/C++ not specific to Visual C++. Can anybody explain what is the purpose of following code: void Function1(int a, int b) { void Function2(int c, int d, int e); // in some header file void Function3(int c, int d, int e); // in some header file int i, k, l; //rest of the code in which Function2 and Function3 are being used! } I have no idea why "Function2" & "Function3" prototypes are defined in the function named "Function1", and is it possible? If yes whats the logic behind it? Any help is highly appreciated. Regards, Adeel
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
This is just a function declaration. Its purpose is to tell the compiler that in some other place( may be in another file or in the same file ) we have a function defined with name
Function2
andFunction3
with following prototype. Other wise the compiler will thow compilation error saying theFunction2
andFunction3
cannot be found. -
This is just a function declaration. Its purpose is to tell the compiler that in some other place( may be in another file or in the same file ) we have a function defined with name
Function2
andFunction3
with following prototype. Other wise the compiler will thow compilation error saying theFunction2
andFunction3
cannot be found.That means, logically it is equal to: void Function2(int c, int d, int e); void Function3(int c, int d, int e); void Function1(int a, int b) { //actual code } Right? Regards, Adeel
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
-
That means, logically it is equal to: void Function2(int c, int d, int e); void Function3(int c, int d, int e); void Function1(int a, int b) { //actual code } Right? Regards, Adeel
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
Ya almost. But the difference is that, now all the functions after the Function1 will be aware of the Function2 and Function3. If the declation was inside the Function1, only Function1 will be aware of the Function2 and Function3. Trying to call those functions from any other function will create compilation error. That is the scope of the declaration is restricted to the body of Function1.