Why can't I put a function after main, visual studio cannot build the program. Is this a C++ quirk or a Visual Studio quirk?
eg.
int main()
{
myFunction()
}
myFunction(){}
will produce an error that main cannot use myFunction
If you define function after main() then you must provides it's prototype before main() first, ( so then after main , you can provide defination ).
For a function defined in the same file where main is defined: If you define it before main , you only have to define it; you don't have to declare it and separately define it. If you define it after main , you have to put a matching prototype declaration before main .
In C you usually define function prototypes in header files, then include the header files, so functions can safely be defined after they are called.
No. One cannot directly define method in methods in java.
most of the computer programming languages have top to down approach which means code is compiled from the top. When we define a function after main function and use it in the main [myFunction () ], compiler thinks " what is this. I never saw this before " and it generates an error saying myFunction not declared. If you want to use in this way you should give a prototype for that function before you define main function. But some compilers accept without a prototype.
#include<stdio.h>
void myFunction(); //prototype
int main()
{
myFunction(); //use
}
myFunction(){ //definition
.......;
}
You can, but you have to declare it beforehand:
void myFunction(); // declaration
int main()
{
myFunction();
}
void myFunction(){} // definition
Note that a function needs a return type. If the function does not return anything, that type must be void
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With