C++: Keeping Function after Main function and still working.?
-
Well, I was using C++ when I found out that you need to put a function before the function declaring it otherwise it won't work. Is there any way to declare the Main Function first, then go to the other function (which is placed after the main function)? Otherwise it will become complicated because I have two functions that link to each other but depending on what you do in the program, one of them will come first. Help is appreciated greatly.
-
Answer:
learn about function prototypes. a function prototype is a declaration of function that gives to the compiler necessary information before actually running it. void foo( int x ) ; //prototype void main() { //Code //................. //CALL foo( ) foo() ; //Code //.............. }//End main() //Now define: the foo() function void foo ( int x ) { cout<< x ; }
John Terrance at Yahoo! Answers Visit the source
Other answers
Yes. You are allowed to declare a function before defining it. For instance, the following is perfectly legal: void whatever(int n); int main() { whatever(17); } void whatever(int n) { //do something } Interestingly, this also makes it possible for two functions to call each other. For instance: void func1(int n); void func2(int n) { if(n>0) { func1(n-1); } } void func1(int n) { if(n>0) { func2(n-1); } }
green meklar
Yes it is: void acoolfunction(int aint); //here you show that the function is there before showing it //whats there void main(){ Mainstuff; acoolfunction(Numberstuff); } //when you use { and } you basicly telling the computer that This is what inside there void acoolfunction(int aint){ FUNCTION STUFF; }
Vermacian55
You can DECLARE functions before the main() function then IMPLEMENT them afterwards. Try #include <iostream> using namespace std; typedef Board{...} void GetBoard(Board &Lifeboard); int Alive(Board Lifeboard); int main(){ ... return 0; } void GetBoard(Board &Lifeboard){ int i, j; for (i=0;i<MAXSIZE;i+=1)... Got it?
jplatt39
Usa a forward declaration int foo(int); .... int main(){ int x; int y; .... y=foo(x); .... return 0; } int foo(int n){ return 0; }
roger
Related Q & A:
- How to implement C callback function in Swift?Best solution by pr8x.com
- How to learn C# (moving from C++ to C#?Best solution by stackoverflow.com
- Is the United States still Israel's main friend and ally?Best solution by foreignpolicy.com
- Is your Yahoo messenger still working?
- Is Paypal still working in India?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.